資料庫常用查詢語句
一:聚合函式
avg(num)平均值 sum(num)求和 max(num)最大值 min(num)最小值 count(*)行數
二:宣告
where屬於約束宣告,對資料表中的資料篩選,不能使用聚合函式
having屬於過濾宣告,聚合函式先執行,對查詢出來的結果篩選。
如 select count(name) as num from A group by name having num>2;
三:子查詢
1.where子句中使用子查詢
select score from A where score> (select avg(score) from A);//查詢成績多餘平均分的人員。
2.from中子查詢
SELECT b.login_id FROM (SELECT u.login_id,u.code FROM base_admin_users u,base_city c where u.city_id=c.id) as b;
查詢出使用者表和能和城市表對應的使用者的賬號
3.where中
SELECT login_id from base_admin_users where login_id not in (SELECT u.login_id FROM base_admin_users u,base_city c where u.city_id=c.id) ;
查詢出使用者表中存在的城市表中不存在的賬號
四:exists
SELECT login_id from base_admin_users e where exists (select 1 from base_city c where e.city_id=c.id and c.pid=0);
查詢pid是0的賬號資訊;
SELECT login_id from base_admin_users e where not exists (select 1 from base_city c where e.city_id=c.id and c.pid=0);
查詢pid不是0的賬號資訊;