標籤:
1.簡單查詢
select * from info;
select Code as ‘代號‘,name as ‘姓名‘ from info;
2.條件查詢
select * from car where code = ‘c002‘;
select * from car where brand =‘b001‘ and power = 130; #或用or
3.模糊查詢
select * from car where name like ‘%奧迪%‘; %代表任意多個字元包括0個 _代表一個字元
4.排序查詢
select * from car order by brand, powers desc; asc升序預設可以不寫
5。範圍查詢
select * from car where price between 40 and 60;
6.離散查詢
select * from car where code in(‘coo1‘,‘c003‘,‘c005‘,‘c007‘);
select * from car where code not in(‘coo1‘,‘c003‘,‘c005‘,‘c007‘);
7.統計查詢(彙總函式)
select count(code) from car; 查詢資料條數也可以count(*)這麼寫,效能低
select sum(price) from car; 求和
select max(code) from car; 最大
select min(brand) from car; 最小
select avg(price) from car; 平均
8.分頁查詢
select * from Car limit (n-1)*5,5 #第n頁,每頁顯示五條資料
9.去重查詢
select distinct Brand from Car
10.分組查詢
select brand,count(*),Brand from Car group by Brand #把brand列的相同值組合起來,並這一條資料對該列或或其他列進行操作例如求平均值
select Brand from Car group by Brand having count(*)>3 #分組之後根據條件查詢使用having 不使用where
MySql各種查詢