分类: Mysql/postgreSQL
2015-06-08 14:35:33
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)
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)
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)