mysql數據庫—表的查詢排序操作大全


表的查詢操作

  select
* from*效率低   select * fromwhere id > 1   select nid,name,gender as gg fromwhere id > 1    as相當於取別名,別名為gg   select count(id) from userinfo5; 查詢表內有多少條數據   select * from tb12; select id,name from tb12; select id,name from tb12 where id > 10 or name ='xxx'; select id,name as cname from tb12 where id > 10 or name ='xxx'; select name,age,11 from tb12; 多加了一列11 其他: select * from tb12 where id != 1 select * from tb12 where id in (1,5,12); select * from tb12 where id not in (1,5,12); select * from tb12 where id in (select id from tb11) select * from tb12 where id between 5 and 12; 閉區間 通配符: select * from tb12 where name like "a%" 以a開頭的所有(多個字符) select * from tb12 where name like "a_" 以a開頭的所有(一個字符) 分頁: select * from tb12 limit 10; select * from tb12 limit 0,10; 0開始位置行,10查看的行數 select * from tb12 limit 10,10; select * from tb12 limit 20,10; select * from tb12 limit 10 offset 20; 20開始位置行,10查看的行數 排序: select * from tb12 order by id desc; 大到小 select * from tb12 order by id asc; 小到大 select * from tb12 order by age desc,id desc; 按優先級安序排列 取后10條數據 select * from tb12 order by id desc limit 10; 分組: select count(id),part_id from userinfo5 group by part_id; 統計數量 select max(id),part_id from userinfo5 group by part_id; part_id重合時,id取max select nin(id),part_id from userinfo5 group by part_id; 聚合函數(去除重復) count max min sum avg 平均值 **** 如果對於聚合函數結果進行二次篩選時?必須使用having **** select count(id),part_id from userinfo5 group by part_id having count(id) > 1; select count(id),part_id from userinfo5 where id > 0 group by part_id having count(id) > 1; 連表操作: select * from userinfo5,department5 where userinfo5.part_id = department5.id select * from userinfo5 left join department5 on userinfo5.part_id = department5.id # 左連接:userinfo5右邊允許有空 select * from userinfo5 right join department5 on userinfo5.part_id = department5.id # 右連接:department5左邊允許有空 select * from userinfo5 inner join department5 on userinfo5.part_id = department5.id # 內連接:不會出現null select * from userinfo5 full join department5 on userinfo5.part_id = department5.id # 完全連接:左右都可能出現null g、組合(上下連表) 組合,自動處理重合 select nickname from A union select name from B 組合,不處理重合 select nickname from A union all select name from B

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM