MySQL學習之路(五)MySQL高級查詢
阿新 • • 發佈:2017-08-29
code left 功能 限定查詢 外鏈接 spa size 平均數 asc
MySQL統計函數
count():統計數量;
max():統計最大值;
min():統計最小值;
avg():統計平均數;
sum():統計和;
Select count(*) from student;
MySQL排序 group by; order by;
默認是升序排序;
Select * from student group by sid asc;--升序排序 Select * from student group by sid DESC;--降序排序 Select * from student group by sid,sname;---多字段排序
having使用方法和where相同,功能比where強大;
limit 限定查詢
limit 查詢數據表的指定數量的數據
limit 只能限定查詢
主要用來實現分頁效果;
Select * from student limit 1; --查詢1條數據 Select * from student limit 1,2; --從下標1開始查詢兩條數據;
MySQL連接查詢
交叉連接:cross join;從一張表中循環取出每一條記錄,每一條記錄都會跟另一張表進行連接匹配;
連接方式: 左表 cross join 右表;
內連接:左表 inner join 右表 on 條件;
別名:as(可以省去as)
外鏈接:outer join;
外左連接;left join;
外右連接:right join;
Select * from student cross join student2; Select * from student inner join student2 on student.sid = student2.sid; Select * from student as a inner join student2 b on a.sid = b.sid; Select * from student as a left outerjoin student2 b on a.sid = b.sid; Select * from student as a right outer join student2 b on a.sid = b.sid;
多表查詢 union(all;distinct)
all:不去重;保留所有數據;
distinct:去重 (默認);
MySQL學習之路(五)MySQL高級查詢