標籤:減法 _id post 底線 最大 int 必須 img gpo
索引唯一索引
唯一: 約束不能重複(可以為空白) ps:主鍵:唯一且不可為空 加速尋找
為多對多 SQL語句補充:增create table tb11( id int auto_increment primary key, name varchar(32), age int)engine=innodb default charset=utf8; insert into tb12(name,age) values(‘alex‘,12); insert into tb12(name,age) values(‘alex‘,12),(‘root‘,18); insert into tb12(name,age) select name,age from tb11; #將tb11表中的name和age插入到tb表裡刪delete from tb12;delete from tb12 where id =2;delete from tb12 where id !=2;delete from tb12 where id !=2 and name=‘alex‘;改update tb12 set name=‘alex‘,where id>12 and name=‘xx‘;update tb12 set name=‘alex‘,age =19 where id>12 and name=‘xx‘;查 select * from tb12;select id,name from tb12;select id,name from tb12 where id >10 or name = ‘xxxx‘;select id,name as cname from tb12 where id >10 or name = ‘xxxx‘; #給輸出的結果,列名設定別名。select id,name,123 from tb12 where id >10 or name = ‘xxxx‘; 結果後面加了一列123 mysql中兩種不等於的寫法:
select id,name from tb12 where id in (1,5,12);select id,name from tb12 where id not in (1,5,12);select id,name from tb12 where id between 5 and 12; 查詢id在5-12之間內容,包含5和12 mysql萬用字元以a開頭:a% %代表無數個內容a_ _代表一個內容 select * from tb12 where name like "a%"select * from tb12 limit 0,10; 0是起始位置,10是起始位置開始顯示幾條。 排序:order by 預設是升序排序 select * from tb12 order by id desc; 從大到小select * from tb12 order by id asc; 從小到大 select PROD_DESC,PROD_ID,COST from PRODUCTS_TBL where COST<20 order by 1;1就是PROD_DESC2是PROD_ID3是COST可以用數字來代替。 select * from tb12 order by age desc,id desc; 按age從大到小排,如果有重複讓重複的再按id從大到小排。
分組:group bygroup by 在where之後 order by 之前select max(id),part_id from userinfo5 group by part_id; 取max(id)和part_id兩列,按part_id分組,max取最大,min取最小 COUNT函數select count(id),part_id from userinfo5 group by part_id; count取個數。count更有可讀性的寫法:select count(part_id ) from userinfo5 group by part_id;注意:count(*)返回的結果包括重複項和NULL。count統計的是行數,不涉及資料類型。 SUM函數SUM函數只能處理數值欄位。select SUM(COST) from PRODUCTS_TBL; AVGFunction Compute平均值,只能處理數字欄位select AVG(SALARY) from EMPLOYEE_PAY_TBL; MAX函數返回記錄中某個最大的值,NULL不在計算範圍內 MIN函數最小值 彙總函數和DISTINCT命令通常不一起使用。 sql語句,如果group by分組之後後面需要彙總函式則必須使用having,如:select count(id),max(id),part_id from userinfo5 goup by part_id having count(id)>1; sql語句where後面不能加彙總函式的結果。 去重:select distinct PROD_DESC from CANDY_TBL; 結果去重 連表操作:select * from userinfo5,department5select * from userinfo5,department5 where userinfo5.part_id = department5.id;將兩張表內容再一張表中顯示 select * from userinfo5 left join department5 on userinfo5.part_id = department5.id和上面效果一樣,兩張表在一張表顯示,第二張表顯示的順序條件是按照userinfo5.part_id = department5.idleft join 左邊的表會全部顯示,右邊不會全部顯示。right join 右邊的表會全部顯示,左邊不會全部顯示。不顯示的為NULL right可以不用,可以用left,但是把兩張表位置調換一下,效果可right是一樣的。 表和表之間必須有關係,有關係才能連表查詢,外鍵等等。 如果通過連表連查多張表,注意列名重複的問題,需要帶上表名.列名 這樣。如:
inner join 和left right的區別就是,會將出現null時的一行隱藏。 select count(id) from userinfo5; 數行數 Mysql邏輯操作符IS NULL 與NULL比較BETWEEN 包括邊界值,between 2 and 3;包括2和3IN 如where PROD_ID in ("13","9","5");LIKE 用萬用字元匹配,%匹配0個一個或多個數字或字元 _底線代表一個數字或字元EXISTS 用於搜尋指定表裡是否存在滿足特定條件的記錄如:select COSTfrom PRODUCTS_TBLwhere exists(select COST from PRODUCTS_TBL where COST > 100); UNIQUEALL 讓一個值和一個集合全部比較 ANY 讓一個值和一個集合任意元素比較,如:select *from PRODUCTS_TBLwhere COST > any(select COST from PRODUCTS_TBL where COST < 100); 算數操作符 +加法-減法*乘法/ 除法例如:select SALARY + BONUS from EMPLOYEE_PAY_TBL;select SALARY - BONUS from EMPLOYEE_PAY_TBL;select SALARY * 10 from EMPLOYEE_PAY_TBL;
mysql知識點回顧(二)