標籤:範圍查詢 平均值 sel between 大於 彙總 mil car 排列
查詢 列 select.... from...
一 .簡單查詢 (查所有資料)
select*from表名 註: * 查所有的列
--------------------------------------------------------------------------------------
二.查詢指定列的資料 (查詢結果是虛擬)
select 列名,列名from 表名
例子:select code,name from info;
三.修改結果集的列名
select code as ‘代號’,name as ‘姓名’ from info
----------------------------查詢行-----------------------------------------------
四.出啊訊指定行的資料
select *form info where code=’p003’;
五 .多條件查詢
查詢 info表中 code為p003 或者 nation=‘n001’的
select *form info where code=’p003’ or nation=’n001’;
查詢 info表中 code為p003 並且 nation=‘n001’的
select *form info where code=’p003’ and nation=’n001’;
六. 範圍查詢
select * from info where price>=40 and price<=70;
select * from info where price between 40 and 70;
------------------------------------------------------------------------in
七 . 離散查詢
查詢汽車價格(20,32,423,54,657,787)內的所有車
select *from info where price in(20,32,423,54,657,787);
查詢汽車價格不在(20,32,423,54,657,787)內的所有車
select *from info where price not in(20,32,3,54,657,787);
-----------------------------------------------------------------------like
八. 模糊查詢
查詢表裡的名稱還有 奧迪的
select*from car where name like ‘%奧迪%’ % 表示任意n 個字元
查詢汽車表中名稱第二個字元為馬的
select * from car where name like’_馬’ _表示一個字元
九 .排序查詢
價格升序排列
select*from car order by price asc asc升序 (可以省略)
價格降序排
select*from car order by price desc asc升序 (可以省略)
先按 brand 排列 再按 price 排列
select*from car order by brand,price,desc;
十. 去重查詢
select distance brand from car;
十一;
一頁顯示10條 當前是 第 三頁
select*from car limit 20,10
---------------------------------------------------------------------------------------------------------------------
十二. 彙總函式 (統計函數)
select count(*) from chinastates #查詢資料總條數
select count(areacode) from chinastates #查詢資料總條數 括弧呢 變成主鍵列 提高運行效率
select count(areacode) from chinastates #查詢資料總條數 括弧呢 變成主鍵列 提高運行效率
select sum(price) from car 求和
select ave(price) from car 平均值
select max(price) from car 最大
select min(price) from car 最小
---------------------------------group by.......having-------------------------------------------------------------
十三. 分組查詢
查詢汽車表中每個系列下有多少個汽車
select brand,count(*) from car group by brand ;
查詢車店 賣的汽車 數量大於4的
select brand from car group by brand having count(*)>3;
Mysql 基礎3