本文共 1995 字,大约阅读时间需要 6 分钟。
只有select语句可以使用它,而且必须放在select之后。
例子:select DISTINCT mobile,phone from js_sys_user
例子:升序:select * from js_sys_user ORDER BY pwd_update_date
降序:select * from js_sys_user ORDER BY pwd_update_date DESC
select mobile,phone from js_sys_user limit 3
聚拢查询大多配合聚拢函数使用。常用的聚拢函数有:count,sum,avg,max,min。
例子:select count(*),SUM(mobile),avg(mobile) from js_sys_user GROUP BY user_type
Having表示对聚拢后的内容进行再条件过滤。
例子:select count(*) from js_sys_user GROUP BY user_type HAVING count(*)>3
例子:select email,role_code from js_sys_user jsu,js_sys_user_role jsur where jsu.user_code=jsur.user_code
左连接:left join…..on
含义:以左边的表为主表,主表的所有查询记录无论有没有和右表匹配,都会查询出来,而右表只有当它和左表匹配时才可以被查询出来。
例子:select user_name,role_code from js_sys_user jsu LEFT JOIN js_sys_user_role jsur on jsu.user_code=jsur.user_code
右连接:right join…..on
含义:以右边的表为主表,主表的所有查询记录无论有没有和左表匹配,都会查询出来,而左表只有当它和右表匹配时才可以被查询出来。
例子:select user_name,role_code from js_sys_user jsu LEFT JOIN js_sys_user_role jsur on jsu.user_code=jsur.user_code
重点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
In与exists的区别:
例如有查询语句:Select * from A where id in(select id from B)
-对于in来说,in是先对B表进行查询,得到查询结果后与A表做笛卡儿积进行匹配。因此当B表的数据比A表多的时候,使用in的数据库查询效率比较高。
-对于exists来说,exists是先对A表进行查询,得到查询结果后与B表做笛卡儿积进行匹配。因此当A表的数据比B表多的时候,使用exists的数据库查询效率更高。
-A、B表数据差不多的时候,两者查询效率相近。
例子:select * from js_sys_area where area_code BETWEEN 370000 and 370111
例子:select * from js_sys_user where email is null
左模糊查询:select * from js_sys_user where user_name like '%0'
右模糊查询:select * from js_sys_user where user_name like '用户%'(右模糊查询能够索引,不需要查询全表,因此查询效率是最高的)
全模糊查询:select * from js_sys_user where user_name like '%管理员%'
能够获得匹配正则的内容。
例子:select * from js_sys_user where password REGEXP 'ba'
转载地址:http://beshn.baihongyu.com/