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 = 52)
/* not exists 子查询是否没有记录(没有查到数据,不是字段),没有有则查询主表中所有的数据,有则显示为null */
select * from os_customer oc where not exists(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)