mysql--浅谈查询1

这是对自己学习燕十八老师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 '诺基亚%';

 select5种语句之group语句

# 作用:分组
# 语法
select 查询项 from 表名
group by 列名(字段);
注意:查询项必须是分组中有的或者统计函数等函数处理后的结果,否则语意上不对
# 示例
select goods_id,cat_id,goods_name
from goods group by cat_id;

常配合group语句使用的统计函数(统计函数也可以单独使用)

1、max:求最大值

#示例
# 查询第三个栏目下最贵的价格
select goods_id,cat_id,max(shop_price),goods_name 
from goods
group by cat_id;

2、min:求最小值

#示例
# 查询第三个栏目下最便宜的价格
select cat_id,min(shop_price),goods_name 
from goods
group by cat_id;

3、sum:求总和

# 一次计算完每个栏目下的库存量之和
select cat_id,sum(goods_number)
from goods
group by cat_id;

4、avg:求平均值

# 按cat_id分组 计算每个栏目下的商品的平均价格
select cat_id,avg(shop_price)
from goods
group by cat_id;

5、求总行数

# select count(*) from 表名:绝对行数,包括null行
# select count(列名) from 表名:不包括null行的行数
# Innodb引擎的表用count(*)效率低 每一行都去数

示例
select count(goods_id) 
from goods;

 select5种语句之having语句

having和where的异同

相同点:两者都是可以筛选数据

不同点:where针对表中的列发挥作用,查询数据

    having针对查询结果中的列发挥作用,筛选数据

# 语法
select 查询项 from 表名 having 表达式

# 示例
# 取出折扣大于200的,类型=3
# as 后面的变量是对前面表达式的命名
select goods_id,cat_id,(market_price-shop_price) as discount
from goods
where cat_id=3
having discount>200;

 select5种语句之order语句

desc:降序排列

asc:升序排列

# 作用:排序
# 语法
select 查询项 from 表名 order by 列名 desc/asc

# 示例
# 多字段排序
select goods_id,cat_id,shop_price,goods_name
from goods
order by cat_id desc,shop_price asc;

 select5种语句之limit语句

# 作用: 限制条数
# 语法
select 查询项 from 表名 limit 偏移量,条目量

# 示例
# 查询出goods表中的前5条数据
select goods_id,cat_id,shop_price,goods_name
from goods
limit 5;

# 查询出goods表中价格在前3到前5的商品
select goods_id,cat_id,shop_price,goods_name
from goods
order by shop_price desc
limit 2,3;

select5种语句之间的使用顺序关系

where <——— group by <———— having <———— order by <———— limit

如果出现了多个查询语句顺序必须如上(等级从左到右依次递减)

小练习:

有如下表及数据

 要求:查询出2门及2门以上不及格者的平均成绩

# 数字之间的比较运算符结果为1/0

select name,sum(score<60) as gk, avg(score) as avg_score
from stu
group by name
having gk>=2;
由于mysql版本的变化,语法可能存在一定的变化,欢迎指出错误和评论区讨论
打破当前不满足的方式就是当下默默努力!!!
原文地址:https://www.cnblogs.com/Pang-Jie/p/10909348.html