延續昨天的內容介紹活動的新增改刪以及表之間的查詢。
語法:
INSERT INTO table_name (column1, column2...)
VALUES (value1, value2...);
INSERT INTO act (status,start_time,end_time,platform_id,updated_at,created_at) values(1,1611504000,1632931200,10868213102191,1632499200,1632499200);
語法:
UPDATE table_name
SET column1=value1, column2=value2, ···
[WHERE Clause];
UPDATE act SET status = 5 WHERE actives_id = 1 ;
語法:
DELETE FROM table_name
[WHERE Clause];
ps.沒加WHERE條件會資料全刪除。
DELETE FROM act WHERE actives_id = 1 ;
(補充)
問題: 當我們不確定資料是否已經存在而進行insert動作,會碰到資料已存在的問題而失敗。
假設我新增了一組相同PK的紀錄,就算後面欄位值有變化只要PK重複就無法新增(因為PK的唯一性)。
mysql> INSERT INTO act (actives_id,status,start_time,end_time,platform_id,updated_at,created_at) values(1,1,1611504000,1632931200,10868213102191,1632499200,1632499200);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
如果遇到上述問題,一般來說我們可以先SELECT在做INSERT去避免此狀況,不過會變成要寫2句語法~
這時就可以運用INSERT ... ON DUPLICATE KEY UPDATE語法一行就能實現功能。
資料不存在 -> INSERT
資料已存在 -> UPDATE
語法: INSERT ... ON DUPLICATE KEY UPDATE
功能: 判斷該主鍵是否重覆,如果重覆就會使用UPDATE該紀錄來完成。
必要條件:
資料表中必須有 PRIMARY/ UNIQUE , INSERT 部分要帶入主索引的欄位資料。
INSERT INTO act (actives_id,status,start_time,end_time,platform_id,updated_at,created_at)
values(1,1,1611504000,1632931200,10868213102191,1632499200,1632499200) ON DUPLICATE KEY UPDATE `status`=2 ;
上面這句等於當actives_id = 1的紀錄已存在的話,就更新status為2 。反之不存在則新增該紀錄
假設我要查詢遊戲名稱中包含ITHOME的紀錄。
語法:
(1)包含 .... WHERE field LIKE pattern
(2)不包含 .... WHERE field NOT LIKE pattern
ps. 搭配%使用 ex. %ITHOME (名稱字尾為ITHOME), %ITHOME% (名稱中包含ITHOME)
mysql> select * from act_game;
+------------+-----------------+-------------------------------+------+------------+
| actives_id | game_name | support_rules | type | created_at |
+------------+-----------------+-------------------------------+------+------------+
| 1 | INVEST-ITHOME21 | ["w102","w103","q200","z920"] | 7 | 1632499200 |
+------------+-----------------+-------------------------------+------+------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM act_game WHERE game_name like '%ITHOME%';
+------------+-----------------+-------------------------------+------+------------+
| actives_id | game_name | support_rules | type | created_at |
+------------+-----------------+-------------------------------+------+------------+
| 1 | INVEST-ITHOME21 | ["w102","w103","q200","z920"] | 7 | 1632499200 |
+------------+-----------------+-------------------------------+------+------------+
1 row in set (0.00 sec)
mysql> SELECT * FROM act_game WHERE game_name not like '%ITHOME%';
Empty set (0.00 sec)
注意: 在語句使用like模糊匹配查詢的方式時,會有幾種造成吃不到INDEX的問題!
注意以下各種查詢方式有無吃到game索引~
假設我要查詢這次的活動紀錄建立時間先後順序(降序排列)。
語法:
SELECT column1, column2...
FROM table_name
ORDER BY column_name1 ASC|DESC, column_name2 ...
使用: ASC (升序排列) / DESC (降序排列)
mysql> SELECT * FROM act ORDER BY created_at DESC ;
假設我要查詢這次的活動紀錄一共有幾種活動狀態(status)。
語法:
SELECT column1, column2...
FROM table_name
[WHERE Clause]
GROUP BY column_name1, column_name2...;
mysql> SELETE status FROM act GROUP BY status;
+--------+
| status |
+--------+
| 0 |
| 1 |
| 2 |
+--------+
3 rows in set (0.01 sec)
最後我們以用於處理邏輯判斷的CASE作為結尾。
假設我要查詢這次的活動紀錄狀態是上架(1) or 未上架(0) or 準備中(N)
語法:
CASE
WHEN condition THEN result
WHEN···
ELSE result
END;
SELECT actives_id, CASE
WHEN status = 0 THEN 'OFF'
WHEN status = 1 THEN 'ON'
ELSE 'READY'
END AS status
FROM act;
+------------+--------+
| actives_id | status |
+------------+--------+
| 1 | ON |
+------------+--------+
1 row in set (0.00 sec)
明天來說一下應用上常用的 表與表之間的連結查詢(JOIN)~