在数据库 MySQL version 5.5.24-log
中,我有两个表 tbl_t1
和 tbl_t2
。
此表格的共同点是 area
。
这是真的,因为如果尝试这个连接查询输出是正确的:
mysql> SELECT
areat2,
areat1,
nameareat1,
monthareat2
FROM
tbl_t1 t1
JOIN tbl_t2 t2 ON t1.areat1 = t2.areat2
WHERE
monthareat2 = '2014-08'
GROUP BY
t1.areat1
ORDER BY
t1.nameareat1 ASC;
+--------+--------+-----------------------+-------------+
| areat2 | areat1 | nameareat1 | monthareat2 |
+--------+--------+-----------------------+-------------+
| XX4M | XX4M | AREA AAAAAA | 2014-08 |
| XX5M | XX5M | AREA BBBBBB | 2014-08 |
| XX4N | XX4N | AREA CCCCCC | 2014-08 |
| XX1M | XX1M | AREA DDDDDD | 2014-08 |
+--------+--------+-----------------------+-------------+
4 rows in set
现在,我需要更新表格 valid
中的字段 tbl_t1
,当两个表格的密钥区域出现时。
我试过这个解决方案,但是我有错误。
UPDATE tbl_t1 t1
JOIN (
SELECT
areat2,
areat1,
nameareat1,
monthareat2
FROM
tbl_t1 t1
JOIN tbl_t2 t2 ON t1.areat1 = t2.areat2
WHERE
monthareat2 = '2014-08'
GROUP BY
t1.areat1
) AS t2 ON t1.areat1 = t2.areat2
SET t1.valid = 1;
[Err] 1060 - Duplicate column name 'areat1'
你能解释一下这是怎么回事吗?
提前谢谢。
答案 0 :(得分:0)
在内部和外部查询中使用不同的别名:
UPDATE tbl_t1 t3
JOIN (
SELECT
areat2,
areat1,
nameareat1,
monthareat2
FROM
tbl_t1 t1
JOIN tbl_t2 t2 ON t1.areat1 = t2.areat2
WHERE
monthareat2 = '2014-08'
GROUP BY
t1.areat1
) AS t4 ON t3.areat1 = t4.areat2
SET t3.valid = 1;