添加語句
- insert[into] tablename(field1,field2,field3......) values(data1,data2,data2.....), (data1,data2,data2....),(.......);
- replace[into] tablename(field1,field2,field3......) values(data1,data2,data2.....), (data1,data2,data2....),(.......);(說明:當插入的數據的主鍵或者唯一鍵有重復的時候,變成修改該行數據)
刪除語句
- delete from tablename (where條件)(order by排序字段)(limit限定)
修改語句
- update tablename set field1=value1,field2=value2,field3=value3,......(where條件)(order by排序字段)(limit限制行數)
查詢語句
- select (all | distinct) 字段或者表達式 (from子句) (where子句) (group by子句) (having子句) (order by子句) (limit子句);
結構解釋:
1.from子句:構成提供給select的數據源,所以一般后面寫表名,例如:
select * from tablename1;
select * from tablename1,tablename2;
2.where子句:where子句是對數據源中的數據進行過濾的條件設置,那么表示where子句是依賴于獲取數據源的from子句的,where子句中可以使用各種"運算符",例如:
select * from tablename where uid>100 and age<50;
select * from tablename where not(city='1');
select * from tablename where cid>100 or sex=0;
//需要注意的運算符 is in like between
//is
select * from tablename where telephone is not null;
//between 判斷數據在某個連續的范圍內(比較適合數字類型的數據)
select * from tablename where age between 20 and 60
//in 字段的值在是所列出的值中的一個,可以用于一些無序的散列值
select * from tablename where rank in (1,3,5,7);
//like 對字符串進行模糊查找
//語法格式: field like "%keyword%"
//content like "%gong%" 查找包含gong的所有數據
//content like "%gong" 查找以gong結尾的所有數據
//content like "gong%" 查找以gong開頭的所有數據
//content like "gong_" 查找以gong開頭的并且只有兩個字符長度的所有數據
//content like "_gong" 查找以gong結尾的并且只有兩個字符長度的所有數據
select * from tablename where username like "%gong%";
3.group by子句:對通過where子句過濾后的數據根某種標準(依據)進行分組
//語法結構 group by field1[desc|asc],field2[desc|asc]
select * from user where uid>100 and sex=0 group by viprank;
4.having子句:對分組后的的數據進行過濾,作用和where類似
select count(*) as 總量,max(price) as 最高價,min(price) as 最低價,avg(price) as 平均價 from goods group by type having 總量>10;
5.order by子句:對取得的數據結果以某個標準(字段)進行排序,默認是asc(正序)
//語法結構 order by field1 [asc|desc],field2[asc|desc]...
select * from user where telephone is not null order by uid desc;
6.limit子句:對取出來的數據進行數量的限制,從第幾行開始取出來幾行數據
select * from goods where num>0 limit 0,20;
7.comment:
①. all | distinct 用于設置select出來的數據是否消除重復行,默認是all既不消除所有的數據都出來;distinct表示會消除重復行
②. concat()是mysql的系統函數,用于連接多個字符串
③. now()獲取系統當前的時間