mysql實現merge into語法
mysql並不沒有oracle、mssql的merge into語法,但是有個on duplicate key update語法(不是標準的sql語法)可以實現merge into語法
實驗一:更新所有欄位
mysql> select * from dup;
+------+--------+-------+
| id | name | phone |
+------+--------+-------+
| 1 | wujian | 123 |
| 2 | xiay | 1234 |
| 3 | wangj | 12345 |
+------+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from dupnew;
+------+------+-------+
| id | name | phone |
+------+------+-------+
| 1 | xyr | 128 |
| 2 | sy | 0 |
| 5 | wsj | 8684 |
+------+------+-------+
3 rows in set (0.00 sec)
mysql> insert into dup(id,name,phone ) select * from dupnew on duplicate key update name=values(name);
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from dup;
+----+-------+-------+
| id | name | phone |
+----+-------+-------+
| 1 | xyr | 123 |
| 2 | sy | 1234 |
| 3 | wangj | 12345 |
| 5 | wsj | 8684 |
+----+-------+-------+
4 rows in set (0.00 sec)
結果:實現了將表dupnew更新到表dup中去,存在就更新,不存在就插入
注意:id欄位是主鍵或UNIQUE索引,不然只會插入dupnew表所有行資料
實驗二:更新部分欄位
mysql> select * from dupagn;
+------+------+-------+
| id | name | phone |
+------+------+-------+
| 1 | myy | 1888 |
| 10 | wz | 556 |
+------+------+-------+
2 rows in set (0.00 sec)
mysql> insert into dup(id,name) select id,name from dupagn on duplicate key update name=values(name);
Query OK, 3 rows affected (0.06 sec)
Records: 2 Duplicates: 1 Warnings: 0
mysql> select * from dup;
+----+-------+-------+
| id | name | phone |
+----+-------+-------+
| 1 | myy | 123 |
| 2 | sy | 1234 |
| 3 | wangj | 12345 |
| 5 | wsj | 8684 |
| 10 | wz | NULL |
+----+-------+-------+
5 rows in set (0.00 sec)
結果:實現了只更新name欄位,但是插入的記錄中其他欄位就為空了
實驗一:更新所有欄位
mysql> select * from dup;
+------+--------+-------+
| id | name | phone |
+------+--------+-------+
| 1 | wujian | 123 |
| 2 | xiay | 1234 |
| 3 | wangj | 12345 |
+------+--------+-------+
3 rows in set (0.00 sec)
mysql> select * from dupnew;
+------+------+-------+
| id | name | phone |
+------+------+-------+
| 1 | xyr | 128 |
| 2 | sy | 0 |
| 5 | wsj | 8684 |
+------+------+-------+
3 rows in set (0.00 sec)
mysql> insert into dup(id,name,phone ) select * from dupnew on duplicate key update name=values(name);
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from dup;
+----+-------+-------+
| id | name | phone |
+----+-------+-------+
| 1 | xyr | 123 |
| 2 | sy | 1234 |
| 3 | wangj | 12345 |
| 5 | wsj | 8684 |
+----+-------+-------+
4 rows in set (0.00 sec)
結果:實現了將表dupnew更新到表dup中去,存在就更新,不存在就插入
注意:id欄位是主鍵或UNIQUE索引,不然只會插入dupnew表所有行資料
實驗二:更新部分欄位
mysql> select * from dupagn;
+------+------+-------+
| id | name | phone |
+------+------+-------+
| 1 | myy | 1888 |
| 10 | wz | 556 |
+------+------+-------+
2 rows in set (0.00 sec)
mysql> insert into dup(id,name) select id,name from dupagn on duplicate key update name=values(name);
Query OK, 3 rows affected (0.06 sec)
Records: 2 Duplicates: 1 Warnings: 0
mysql> select * from dup;
+----+-------+-------+
| id | name | phone |
+----+-------+-------+
| 1 | myy | 123 |
| 2 | sy | 1234 |
| 3 | wangj | 12345 |
| 5 | wsj | 8684 |
| 10 | wz | NULL |
+----+-------+-------+
5 rows in set (0.00 sec)
結果:實現了只更新name欄位,但是插入的記錄中其他欄位就為空了
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29989552/viewspace-2109761/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql實現merge功能之DUPLICATE key UPDATE語法MySql
- Merge語法限制
- Oracle Merge語法Oracle
- Oracle merge into delete語法Oracledelete
- SQL 高階語法 MERGE INTOSQL
- MySQL 06 mysql 如何實現類似 oracle 的 merge intoMySqlOracle
- oracle之merge語法(轉載)Oracle
- MERGE語句語法檢查不嚴格
- MySQL基礎語法實踐MySql
- SQL Server 2008 MERGE語法SQLServer
- MogDB/openGauss中merge的語法解析
- mysql mergeMySql
- MYSQL語法(一)MySql
- 常用MySQL語法MySql
- MySQL Join語法MySql
- mysql基本語法MySql
- mysql 語法1MySql
- MySQL:2、MySQL基礎語法MySql
- mysql基礎語法MySql
- 高精度減法(C語言實現)C語言
- MacroPy:Python 的巨集語法實現MacPython
- MacroPy:Python 的宏語法實現MacPython
- MYSQL merge union merge sort_union 的不同MySql
- MySQL 函式語法整理MySql函式
- MySql 常用語法彙總MySql
- ABAP Development Tools的語法高亮實現原理dev
- 用原型實現Class的各項語法原型
- Mysql實現全外部連線(mysql無法使用full join的解決辦法)MySql
- MYSQL基礎語法的使用MySql
- mysql函式定義語法MySql函式
- MySQL-基礎語法教程MySql
- 簡單理解async、await語法實現原理AI
- 簡單語法解析器實現參考
- SAPGUI裡實現自定義的語法檢查GUI
- react在jsx語法中實現for迴圈ReactJS
- C語言實現牛頓迭代法解方程C語言
- 資料庫實現原理#3(Merge Join).md資料庫
- oracle中merge 語句使用Oracle