插入,修改,更改語句
1、insret into 表名[(字段列表)] value(值列表); //插入單條數(shù)據(jù)
2、insert into 表名[(字段列表)] values(值列表1),(值列表2),(......),(值列表n); //插入多條語句
3、replace into 表名[(字段列表)] values(值列表); //使用replace語句插入單條數(shù)據(jù)
4、replace into 表名[(字段列表)] values(值列表1),(值列表2),(......),(值列表n); //使用replace插入多條語句
5、insert into 目標(biāo)數(shù)據(jù)表(字段列表1) select 字段列表2 from 源數(shù)據(jù)表 where 條件表達(dá)式; //將一個(gè)表中查詢出來的數(shù)據(jù)插入到另一個(gè)表中
6、insert into 表名 set 字段名1=值1,字段名2=值2; //插入數(shù)據(jù)
7、update 表名 set 字段名1=值1,字段名2=取值2,…,字段名n=取值n [where 條件表達(dá)式]; //更新數(shù)據(jù)表中數(shù)據(jù)
8、delete from 表名 [where 條件表達(dá)式]; //刪除數(shù)據(jù)
9、truncate [table] 表名; //無條件刪除表
查詢操作
10、select * from 表名; //查詢表中所有屬性
11、select 列名1,列名2,....列名n from 表名; //查詢表中指定列
12、select 列名1,列名2,....列名n from 表名 where 條件; //選擇行查詢
13、select 列名1,列名2,....列名n from 表名 where [not] 表達(dá)式1 邏輯運(yùn)算符 表達(dá)式2; //使用and,or,not三種運(yùn)算符查詢
14、select 列名1,列名2,....列名n from 表名 where 表達(dá)式 [not] between 初始值 and 終止值; //使用BETWEEN AND來限制查詢數(shù)據(jù)的范圍
15、select 列名1,列名2,....列名n from 表名 where 表達(dá)式 [not] in(值1,值2,....值n); //使用in限制查詢數(shù)據(jù)的范圍
16、select 列名1,列名2,....列名n from 表名 where 列名 [not] like '字符串' [escape '轉(zhuǎn)義字符']; //使用like進(jìn)行模糊查詢
17、select 列名1,列名2,....列名n from 表名 where 列名 is [not] null; //查詢表信息為空的或不為空的列
18、select distinct 列名1,列名2,....列名n from 表名 where 條件; //消除重復(fù)結(jié)果集
19、select 列名1,列名2,....列名n from 表名 where 條件 order by 列名x asc; //按列名x升序排列
20、select 列名1,列名2,....列名n from 表名 where 條件 order by 列名y desc; //按列名y降序排列
21、select 列名1,列名2,....列名n from 表名 where 條件 order by 列名x asc 列名y desc; //先按列名x升序排列再按列名y降序排列
22、select 列名1,列名2,....列名n from 表名 limit offset; //offset為可選項(xiàng),默認(rèn)為0,當(dāng)為1時(shí)查詢從第二條開始,依次類推
23、select sum(列名) from 表名; //統(tǒng)計(jì)總數(shù)
24、select count(列名) from 表名; //統(tǒng)計(jì)個(gè)數(shù)
25、select max(列名) from 表名; //返回最大值
26、select min(列名) from 表名; //返回最小值
27、select avg(列名) from 表名; //返回各值的平均值
28、select 列名1,列名2,....列名n from 表名 group by 字段名; //分組統(tǒng)計(jì)
29、select 列名1,[sum][max][min][avg]count(列名) from 表名 group by 列名; //group by和聚合函數(shù)一起用
30、select 列名 form 表名 where 列名=(select 列名 from 表名 where 條件表達(dá)式); //子查詢
31、select 列名 form 表名 where 列名 in(select 列名 from 表名 where 條件表達(dá)式); //in字查詢
32、select 列名1,列名2 from 表名 where 條件表達(dá)式 union select 列名1,列名2 from 表名 where 條件表達(dá)式; //聯(lián)合查詢
33、select u.列名1,s.列名2 from 表名1 u join 表名2 s on u.列名x=s.列名x [where][group by]; //內(nèi)連接,兩表中讀存在列名x
34、select u.列名1,s.列名2 from 表名1 u left join 表名2 s on u.列名x=s.列名x [where] [group by]; //左外連接,兩表中讀存在列名x
35、select u.列名1,s.列名2 from 表名1 u right join 表名2 s on u.列名x=s.列名x [where] [group by]; //右外連接。兩表中讀存在列名x