mysql常用查询语句
2021/11/23 19:10:43
本文主要是介绍mysql常用查询语句,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
mysql常用查询语句
/* in 查询满足指定范围内的条件的记录*/
select id,name from os_customer where id in (12,13,14)
/* not in 查询不满足指定范围内的条件的记录*/
select id,name from os_customer where id not in (12,13,14,15,16)
/between and 查询指定范围内的值,包括端点值,用于查询时间范围时,最好使用>= <=/
select id, name from os_customer where id between 0 and 12
/not between and 查询指定范围外的值,包括端点值/
select id, name from os_customer where id not between 0 and 12
/模糊查询 like “%”匹配任意长度的字符,“_”匹配任意一个字符。/
select id, name from os_customer where name like "%局%"
select id, name from os_customer where name like "%电_%"
/null 值查询,如果一个字段设置为null,可以使用is null 查询/
select id, name, update_time from os_customer where update_time is null
/非null查询 和null刚好相反,使用is not null 查询/
select id, name, update_time from os_customer where update_time is not null
/* 按时间倒序排列,order by 字段 desc/asc 降序/升序排序(order by 字段 后面不加,默认按升序排列) 后面可以跟多个几段,按前后顺序排序 ,但是这样会有些问题,不推荐多个字段*/
select id,name,create_time from os_customer order by create_time desc
select id,name,create_time from os_customer order by create_time
/* 分组 group by 对数据按照某个字段或多个字段进行分组,和集合函数一起使用,MAX(),MIN(),COUNT(),SUM(),AVG()(以分组后的每一组为单位使用上述函数) */
select id ,name,count(1) from sys_user where name like "%家%华%" group by name
select id ,name,create_time from sys_user where name like "%家%华%" group by name,create_time
/HAVING 过滤分组/
select id, name from os_customer group by name having count(name) >1
/* 显示分组后数据的所有记录 */
select name from sys_user where name like "%家%华%" group by name with rollup
/* limit 限制查询结果*/
select * from sys_user limit 4
/* 内连接查询 以下两句结果相同的*/
select * from os_customer oc inner join os_customer_user ocu on oc.id = ocu.id
select * from os_customer oc, os_customer_user ocu where oc.id = ocu.id
/* 左外连接 left join 显示左表所有行,如果右表没有对应的字段为null */
select * from os_customer oc left join os_customer_user ocu on oc.id = ocu.id
/* 右外连接(right join)显示右表中的所有行 ,如果左表没有对应的字段为null*/
select * from os_customer oc right join os_customer_user ocu on oc.id = ocu.id
/* any 返回大于子查询结果中最小值的所有记录 */
select * from os_customer oc where oc.id > any(select user_id from os_customer_user)
/* all 返回大于子查询结果中最大值的所有记录 */
select * from os_customer oc where oc.id > all(select id from os_customer_user)
/* exists 子查询是否有记录(能查到数据,不是字段),有则查询主表中所有的数据,没有则显示为null */
select * from os_customer oc where exists(select id from os_customer_user where id = 2)
select id from os_customer_user where id = 2 这条数据可以查询出数据,因此可以查询到数据
select * from os_customer oc where exists(select id from os_customer_user where id = 52)
select id from os_customer_user where id = 52 这条语句查询不到数据,因此最后查询不到数据
/* not exists 子查询是否没有记录(没有查到数据,不是字段),没有有则查询主表中所有的数据,有则显示为null */
select * from os_customer oc where not exists(select id from os_customer_user where id = 50)
select id from os_customer_user where id = 50 这条查询语句是没有数据的,因此最终的查询可以查到数据
select * from os_customer oc where not exists(select id from os_customer_user where id = 5)
select id from os_customer_user where id = 5 这条查询语句是有数据的,因此最终的查询查不到数据
/in 主表字段在子查询语句中的集合中的数据/
select * from os_customer oc where id in (select user_id from os_customer_user )
/not in 主表字段不在子查询语句中的集合中的数据/
select * from os_customer oc where id not in (select user_id from os_customer_user)
/* <、<=、=、>=、!= 条件查询*/
select * from os_customer oc where id >= 36
/* union 删除重复行 每个union查询字段要相同*/
select id,name from os_customer where id >45 union select id,name from os_customer where id in (48,49)
/* union all 不删除重复行 每个union查询字段要相同*/
select id,name from os_customer where id >45 union all select id,name from os_customer where id in (48,49)
这篇关于mysql常用查询语句的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-25如何部署MySQL集群资料:新手入门教程
- 2024-12-24MySQL集群部署资料:新手入门教程
- 2024-12-24MySQL集群资料详解:新手入门教程
- 2024-12-24MySQL集群部署入门教程
- 2024-12-24部署MySQL集群学习:新手入门教程
- 2024-12-24部署MySQL集群入门:一步一步搭建指南
- 2024-12-07MySQL读写分离入门:轻松掌握数据库读写分离技术
- 2024-12-07MySQL读写分离入门教程
- 2024-12-07MySQL分库分表入门详解
- 2024-12-07MySQL分库分表入门指南