mysql update delete join问题
时间:2010-06-10 来源:ubuntuer
1. update join
update info1 a inner join info2 b on a.id=b.id set a.name=b.name;
2. delete join
delete from info1 , info2 using info1 inner join info2 on info1.id=info2.id;
mysql> desc info1;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | | |
| name | varchar(20) | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> desc info2;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | | | |
| name | varchar(20) | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from info1;
+----+---------+
| id | name |
+----+---------+
| 1 | zj1 |
| 2 | kenthy1 |
| 3 | jimmy1 |
| 4 | jeremy |
+----+---------+
4 rows in set (0.00 sec)
mysql> select * from info2;
+----+---------+
| id | name |
+----+---------+
| 1 | zj2 |
| 2 | kenthy2 |
| 3 | jimmy2 |
+----+---------+
3 rows in set (0.00 sec)
mysql> update info1 a inner join info2 b on a.id=b.id set a.name=b.name;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from info1;
+----+---------+
| id | name |
+----+---------+
| 1 | zj2 |
| 2 | kenthy2 |
| 3 | jimmy2 |
| 4 | jeremy |
+----+---------+
4 rows in set (0.00 sec)
mysql> delete from info1 , info2 using info1 inner join info2 on info1.id=info2.id;
Query OK, 6 rows affected (0.02 sec)
mysql> select * from info1;
+----+--------+
| id | name |
+----+--------+
| 4 | jeremy |
+----+--------+
1 row in set (0.00 sec)
mysql> select * from info2;
Empty set (0.00 sec)