您好,登錄后才能下訂單哦!
這篇文章給大家介紹mysql中通過關(guān)聯(lián)表update數(shù)據(jù)的誤區(qū)測試是怎樣的,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
關(guān)于update關(guān)聯(lián)表的寫法存在很多誤區(qū),以前我自己也經(jīng)常犯錯....
一般的寫法有如下幾種:
update test1 set name =(select name from test2 where test1.id=test2.id);
update test1 a,test2 b set a.name=b.name where a.id=b.id;
update test2 set test2.name=(select name from test1 where test1.id=test2.id) where exists (select * from test1 where test1.id=test2.id);
一般來講這三種寫法都沒問題,只要在test1,和test2中name和id字段都是唯一的...
下面我們分別討論更改表和關(guān)聯(lián)表的情況:
首先,討論被關(guān)聯(lián)表有重復的情況:
mysql> select * from test1;
+------+------+
| id | name |
+------+------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
| 4 | kkk |
| 4 | k4 |
| 4 | k4 |
+------+------+
6 rows in set (0.00 sec)
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1 | 2222 |
| 2 | 2222 |
| 3 | 2222 |
| 4 | 2222 |
+------+------+
4 rows in set (0.00 sec)
mysql> update test2 set name = (select test1.name from test1 where test1.id=test2.id)
-> ;
ERROR 1242 (21000): Subquery returns more than 1 row
mysql> update test2 a,test1 b set a.name=b.name where a.id=b.id;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
| 4 | kkk |
+------+------+
4 rows in set (0.00 sec)
test1表中id=4的記錄有多個...且值也不一致.這個時候?qū)est1做為驅(qū)動表,是不可取的..不管用什么語法,要么會報錯,要么就會只去驅(qū)動表的重復的值的第一個值...
接著討論被更改表有多余值的情況
多余值是相對于更改條件而言的.比如只更改ID1-4的..而存在id=5的類似的情況;
mysql> select * from test2;
+------+---------+
| id | name |
+------+---------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
| 4 | kkk |
| 5 | gaopeng |
+------+---------+
5 rows in set (0.00 sec)
mysql> select * from test1;
+------+------+
| id | name |
+------+------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
+------+------+
3 rows in set (0.00 sec)
mysql> update test2 set test2.name=(select name from test1 where test1.id=test2.id);
Query OK, 2 rows affected (0.01 sec)
Rows matched: 5 Changed: 2 Warnings: 0
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
| 4 | NULL |
| 5 | NULL |
+------+------+
5 rows in set (0.00 sec)
可以看到,如果不加條件的update,會導致多余的數(shù)據(jù)被更改為null.
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1 | rrr |
| 2 | rrr |
| 3 | rrr |
| 4 | rrr |
| 5 | rrr |
+------+------+
5 rows in set (0.00 sec)
mysql> update test2 set test2.name=(select name from test1 where test1.id=test2.id) where exists (select * from test1 where test1.id=test2.id);
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from test2;
+------+------+
| id | name |
+------+------+
| 1 | k1 |
| 2 | k2 |
| 3 | k3 |
| 4 | rrr |
| 5 | rrr |
+------+------+
5 rows in set (0.00 sec)
所以要注意關(guān)聯(lián)更改時要注意的條件:
1.驅(qū)動表不能有重復值.關(guān)聯(lián)表作為參考值,不能有重復,不然取值時不知道取哪一個值
2.被更改表如有多余值時,一定要加條件,不然,沒有被關(guān)聯(lián)到的數(shù)據(jù)會被更改為null;
關(guān)于mysql中通過關(guān)聯(lián)表update數(shù)據(jù)的誤區(qū)測試是怎樣的就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。