这是对自己学习燕十八老师mysql教程的总结,非常感谢燕十八老师。
依赖软件:mysql5.6
系统环境:win
在谈查询之前,先说一个特别重要的概念
一定将列名(字段)看成变量,既然是变量就可以运算
一定将列名(字段)看成变量,既然是变量就可以运算
一定将列名(字段)看成变量,既然是变量就可以运算
重要的事说三遍
select5种语句之where语句
# 语法 select 查询项 from 表名 where 表达式; 表达式为真,则取出对应的查询项 # where应用场景 各种条件查询场合,如按学号查询学生,上线时间查产品等
1、比较运算符
1、等于(=)
# 查询商品id为32的商品 select goods_id,cat_id,goods_name,shop_price from goods where goods_id=32;
2、不等于(!=/<>)
# 查询不属于第三个栏目的商品 select goods_id,cat_id,goods_name from goods where cat_id!=3;
3、大于(>)
# 本店价格高于3000的商品 select goods_id,cat_id,shop_price,goods_name from goods where shop_price>3000;
4、小于(<)
# 本店价格低于或等于100的商品 select goods_id,cat_id,shop_price,goods_name from goods where shop_price<100;
5、在某个集合内(in)------>离散型
# 查出在栏目4或者栏目11内的商品信息 不用or select goods_id,cat_id,goods_name from goods where cat_id in (4,11);
6、在某个范围内(between...and...)------>连续型
# 取出商品价格在100和500之间的商品,包含100,500 不用and select goods_id,cat_id,shop_price,goods_name from goods where shop_price between 100 and 500;
7、不在某个集合内(not in)------>离散型
#查询不在第3栏和不在第11栏的商品 select goods_id,cat_id,good_name from goods where cat_id not in (3,11);
8、列名看成变量并计算
#取出本店价格比市场价省的钱,并且省200以上的 select goods_id,cat_id,market_price-shop_price,goods_name from goods where market_price-shop_price >200;
2、逻辑运算符
1、逻辑与(and / &&)
#查询不在第3栏和不在第11栏的商品 select goods_id,cat_id,goods_name from goods where cat_id!=3 and cat_id!=11;
2、逻辑或(or / ||)
#查询价格大于100且小于300,或者大于3000且小于4000的商品 select goods_id,cat_id,shop_price,goods_name from goods where shop_price between 100 and 300 or shop_price between 3000 and 4000;
3、逻辑非(not / !)
# 查看价格小于500的商品 用>=和not select goods_id,shop_price,goods_name from goods where shop_price not (shop_price>=500);
3、模糊匹配(like)
% 通配任意多个字符
_ 通配任意单一字符
# 查询以诺基亚开头的商品名 select goods_id,cat_id,goods_name from goods where goods_name like '诺基亚%';
在这介绍两个字符串函数
cancat() :字符串拼接函数
substring() : 字符串剪切函数
# 取出诺基亚开头的商品并将诺基亚改成HTC select goods_id,cat_id,concat('HTC',substring(goods_name,4)) from goods where goods_name like '诺基亚%';
# 将诺基亚开头的商品名称改成HTC开头的 update goods set goods_name=concat('HTC',substring(goods_name,4)) where godds_name like '诺基亚%';