一个简单的Update语句

一个简单的Update语句



mysql> create table aa(name varchar(100),grade int(5));
Query OK, 0 rows affected (0.09 sec)

mysql> create table bb(name varchar(100),age int(5));
Query OK, 0 rows affected (0.14 sec)

mysql> insert into aa values('AAA',80);
Query OK, 1 row affected (0.05 sec)

mysql> insert into aa values('BBB',60);
Query OK, 1 row affected (0.06 sec)

mysql> insert into aa values('CCC',60);
Query OK, 1 row affected (0.03 sec)

mysql> insert into bb values('CCC',26);
Query OK, 1 row affected (0.05 sec)

mysql> insert into bb values('BBB',24);
Query OK, 1 row affected (0.03 sec)

mysql> insert into bb values('AAA',26);
Query OK, 1 row affected (0.03 sec)

mysql> select * from aa;
+------+-------+
| name | grade |
+------+-------+
| AAA  |    80 |
| BBB  |    60 |
| CCC  |    60 |
+------+-------+
3 rows in set (0.00 sec)

mysql> select * from bb;
+------+------+
| name | age  |
+------+------+
| CCC  |   26 |
| BBB  |   24 |
| AAA  |   26 |
+------+------+
3 rows in set (0.00 sec)

--用aa表的grade更新bb表的age

mysql> update bb b set age=(select grade from aa a where a.name=b.name);
Query OK, 3 rows affected (0.03 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select * from bb;
+------+------+
| name | age  |
+------+------+
| CCC  |   60 |
| BBB  |   60 |
| AAA  |   80 |
+------+------+
3 rows in set (0.00 sec)

mysql>

你可能感兴趣的:(mysql)