Mysql select語法筆記

2021-10-14 01:21:05 字數 2821 閱讀 7326

*順序

select *

from t

where c1 = x

order by c2

*列運算與重新命名

select

c3,c4+10 as 『newname』

from t

where c1 = x

order by c2

*去重select distinct c1

from t

*時間select *

from t

where c > 『1990-01-01』

*多條件(and > or,not)

select *

from t

where not (c1 > 『1990-01-01』 and c2 >1000)

*多條件(in)

select *

from t

where c in (『va』,『fl』,『ga』)

– where c=『va』 or c=『gl』 or c = 『ga』

*多條件(between)

select *

from t

where c between 1000 and 3000

*萬用字元(like)

select *

from t

where c like 『b%』

– where c like 『%b%』

– where c like 『%b』

– where c like 『_b』(乙個空格表示乙個字元,如ab符合,但aab不符合)

*萬用字元(regexp)

select *

from t

– where c like 『%field%』

– where c regexp 『field』

– where c regexp 『^field』(必須以field開頭)

– where c regexp 『field$』(必須以field結尾)

– where c regexp 『^field | mac | rose』

– where c regexp 『[gim]e』 (包含ge、ie、me的都會被選上。另一種』e[a-h]』,ea、eb…eh都會被選上)

*null操作

select *

from t

where c is null

*order by

select *

from t

order by c2 desc

– order by c1 desc, c2 desc

*limit

select *

from t

limit 3

– limit 6,3(從第6行開始,查詢第7,8,9這三行資料)

*內連線(inner join,預設join就是內連線)

select a.c1,t2.c3

from t1 as a

join t2

on a.c1 = t2.c2

*跨資料庫連線,資料庫1,2分別記為db1,db2

select *

from db1.t1 as a

join db2.t2 as b

on a.c1 = b.c2

*自連線

select e.c1,m.c1

from t e

jpin t m

on e.c1 = m.c2

*多表連線

select *

from t1

join t2

on t1.c1 = t2.c1

join t3

on t1.c2 = t3.c2

*復合連線(復合主鍵情形)

select *

from t1

join t2

on t1.c1 = t2.c1

and t1.c2 = t2.c2

*隱式連線

select *

from t1,t2 (交叉鏈結)

where t1.c1 = t2.c1

*外連線(left/right join)

select *

from t1

left join t2

on t1.c1 = t2.c1

order by t1.c1

*多表外連線

select *

from t1

left join t2

on t1.c1 = t2.c1

left join t3

on t3.c2 = t2.c2

order by t1.c1

*自外連線

select *

from t e

left join t m

on e.c1 = m.c2

*using子句(名字必須一樣)

select *

from t1

join t2

– on t1.c1 = t2.c1

using (c1)

– using (c1,c3)(復合主鍵)

left join t3

using (c2)

*交叉連線(主鍵笛卡爾積)

select *

from t1

cross join t2

– select * from t1,t2

*unions(合併多條查詢記錄)

select c1 as newname

from t1

union

select c2

from t2(列的數目必須一致)

mysql select深入應用一

1 select select from 表名 where 欄位名 查詢的內容 例子 select from t1 where name 張三 其中 號可以用 多個條件查詢 or 或者 and 並且 例子 select from t1 where name 趙四 or id 12 模糊查詢 like...

MYSQL select時鎖定記錄問題

在使用sql時,大都會遇到這樣的問題,你update一條記錄時,需要通過select來檢索出其值或條件,然後在通過這個值來執行修改操作。但當以上操作放到多執行緒中併發處理時會出現問題 某執行緒select 了一條記錄但還沒來得及 update 時,另乙個執行緒仍然可能會進來 select 到同一條記...

小貝 mysql select連線查詢

作用 把2次或多次查詢結果合併起來 詳細 表1查詢結果 union 表2查詢結果 執行 先算表1查詢結果,再算表2查詢結果。再通過union把結果聯合起來。總結 a 左右查詢結果列數一致 b 最終顯示結果以第一張表的列名為主 c 左右查詢結果的列型別最好也一致,不然就會進行轉換。由低到高。如表1結果...