Chinaunix
标题:
求助,多表关联更新问题
[打印本页]
作者:
Linux521
时间:
2016-09-26 09:20
标题:
求助,多表关联更新问题
目前有三个表A(uacct,user_name),B(uid,username),C(uid,realname);
A.user_uacct=B.username
B.uid=C.uid
想取出A.user_name更新到C.realname. 如何写出批量更新的SQL呢,谢谢。
作者:
seesea2517
时间:
2016-09-27 11:02
回复
1#
Linux521
试一下:
mysql> select * from A;
+-------+-----------+
| uacct | user_name |
+-------+-----------+
| aaa | axx |
| bbb | bxx |
| ccc | cxx |
+-------+-----------+
3 rows in set
mysql> select * from B;
+-----+-----------+
| uid | user_name |
+-----+-----------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+-----+-----------+
3 rows in set
mysql> select * from C
;
+-----+----------+
| uid | realname |
+-----+----------+
| aaa | NULL |
| bbb | NULL |
| ccc | NULL |
+-----+----------+
3 rows in set
mysql> update A, B, C set realname = A.user_name where A.uacct = B.user_name and B.uid = C.uid;
Query OK, 3 rows affected
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from C;
+-----+----------+
| uid | realname |
+-----+----------+
| 1 | axx |
| 2 | bxx |
| 3 | cxx |
+-----+----------+
3 rows in set
mysql>
复制代码
作者:
will009
时间:
2016-09-27 11:21
回复
2#
seesea2517
不对吧,你的例子中B.uid=C.uid吗
作者:
seesea2517
时间:
2016-09-27 15:40
回复
3#
will009
感谢指出,那是最初的输出,后来测试发现做错测试数据了,改完后直接测试没重新输出截图了,这是改下后的 uid 数据:
ysql> select * from B;
+-----+-----------+
| uid | user_name |
+-----+-----------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+-----+-----------+
3 rows in set
mysql> select * from C
;
+-----+----------+
| uid | realname |
+-----+----------+
| 1 | axx |
| 2 | bxx |
| 3 | cxx |
+-----+----------+
3 rows in set
复制代码
作者:
Linux521
时间:
2016-10-13 17:03
本帖最后由 Linux521 于 2016-10-13 17:06 编辑
感谢
seesea2517
will009 。已解决,当时想多了点
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2