Select檢索資料
資料查詢語言DQL
簡單查詢 Select語句文法
Select
[distinct | all ]
查詢的欄位列表
from 查詢的表列表
[where 條件陳述式]
[group by 子語 ]
[having 子語 ]
[order by 排序條件 ]
建立一個比較簡單的表來學習簡單sql查詢:
create table test
(
id int ,
Name varchar(50),
num int ,
Time datetime,
Date date
)
INSERT INTO test VALUES (2, '劉崗', 2, '2016-9-18 11:54:26', '2016-6-2');
INSERT INTO test VALUES (3, '李安平', 3, '2016-9-18 11:54:32', '2016-9-14');
INSERT INTO test VALUES (4, '吳平台', 4, '2016-9-18 11:54:36', '2016-7-15');
INSERT INTO test VALUES (5, '張可可', 5, '2016-9-18 11:54:40', '2016-6-16');
INSERT INTO test VALUES (6, '陳如', 6, '2016-9-18 11:54:51', '2016-6-24');
INSERT INTO test VALUES (1, '吳順', 1, '2016-9-18 11:55:15', '2016-8-11');
INSERT INTO test VALUES (7, '李漸立', 7, '2016-9-18 11:55:41', '2016-9-25');
比較子
=
>
>=
<
<=
<>,!= 不等於
select * from test where num >3
查出4,5,6
日期比較
select * from test where date > '2016-08-01'
結果:
邏輯運算子
and
select * from test where date > '2016-08-01' and date <'2016-08-15' and num = 1
結果:
And表示被查詢的資料要滿足所有的條件,才查出資料
Or
select * from test where date > '2016-08-01' or num =2
結果:
Or表示只要一個條件成立就才查出相應的資料
()的運算優先順序
select * from test where num > 2 or date > '2016-06-13' and date < '2016-08-11'
結果:
select * from test where ( num > 2 or date > '2016-06-13' ) and date < '2016-08-11'
結果:
以上兩個sql 用了or ,但是加上 () 和沒加 () 的結果有差別。
()的運算優先順序高於and Not 表示非,取反的意思。Not 放在哪個條件前面,表示後面的一個條件組成的結果取反
select * from test where not num =2
表示num=2 的結果取反
結果:
select * from test where num = 7 or not date > '2016-09-13'
表示date > '2016-09-13' 的結果取反,查出的是小於9.13的資料 而num=7沒有not 就不取反,即便時間大於13,也查出來
結果:
範圍查詢in
select * from test where num = 1 or num = 2 or num = 4
同等於:select * from test where num in (1,2,4)
取反 select * from test where not num in (1,2,4)
Or表示是滿足某些指定條件, 如果條件很多,那麼or就過長冗長,此時用in () 就很方便。
like模糊查詢
%表示通配
select * from test where `name` like '李%'
結果:
select * from test where `name` like '%平%'
結果:
_表示匹配一個字元
select * from test where `name` like '陳_'
結果:
select * from test where `name` like '李__'
結果:
Null查詢
先將一行資料設定為null
update test set num = null where id = 1
select * from test where num is null
結果:
Num是null的資料就會被查出
select * from test where num is not null
結果:
num是null的資料就不會被查出
範圍查詢betwwen ... And
select * from test where num BETWEEN 3 and 6
查出num的值從3到6的,包括3和6
同等於
select * from test where num >=3 and num <= 6
去重複欄位查詢distinct
先修改出一個相同的資料
update test set num =2 where id = 1
select num from test
以上結果中有兩個2
使用DISTINCT 之後,重複的2就會不展示出來,這就是資料去重
select DISTINCT num from test
結果:
as 別名
as 是給列名取別名
select name as 名稱 , num as 數量 from test
結果:
Order by 排序
order by .. desc 降序
order by .. asc 升序
select * from test order by num desc
按 num 降序
select * from test order by num asc
按 num 升序
註:1,null值在排序時被當成是最小值
2,當order by 後不帶desc 或asc時 預設是升序
如:select * from test order by num
結果:
以列的位置來排序
select name , num ,id from test order by 3 asc
表示以查出來的第3列來排序,這裡的第3列是id, 就相當於以id來排序。
結果:
還可以以別名來排序
select name as 名稱 , num as 數量 from test order by 數量 desc
結果: 使用多個欄位排序
select * from test order by num,name asc
按第一個欄位進行排序,當第一個欄位有重複的值時再對這些資料進行第二個欄位排序。 巢狀查詢
設計一個電商購物平台的案例來學習強大的select檢索語句