码迷,mamicode.com
首页 > 数据库 > 详细

mysql-高级查询

时间:2019-11-19 14:00:15      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:min   内连接   -o   where   sel   max   重命名   mit   正则表达   

高级查询

###模糊查询

select * from class where name like ‘to_‘; 下划线代表一个字符
select * from class where name like ‘to%‘;%代表0个或者多个字符
select * from class where name regexp ‘to\w*‘; regexp 后面用正则表达式


###联合查询

select * from class where name=‘chen‘ union[all重复结果也重复显示] select * from class where age>25; #可以不同表一起查询

 

as 另命名使用

select * from (select * from class where age>25) as b where b.name=‘chen‘;

(select * from class where age>25) 查询结果作为一个源表,命名为b使用,字段也可以重命名


select * from class where age > (select age from class where name=‘chen‘);
查询结果作为一个字段的内容使用

 


###聚合查询
select name,min(score) from class where age>20 group by name order by min(score) [desc降/默认升] limit 5;
优秀顺序:
from查询表 --where条件 --group by分组(select 后面字段必须和分组字段一致,或者聚合函数) ---having(分组后的字段判断)
---order by(排序) ---limit(限制显示数量) ------select(显示)

聚合函数:
min(字段) 最小
max()最大
avg()平均
sun()和
count(*)满足查询条件的记录数量

 

 

###表关联查询
select * from class,dept where class.name=dept.name;
select* from class inner join dept on class.name=dept.name; 内连接

select* from class left/right join dept on class.name=dept.name;外连接

 

mysql-高级查询

标签:min   内连接   -o   where   sel   max   重命名   mit   正则表达   

原文地址:https://www.cnblogs.com/chenlulu1122/p/11888692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!