我想知道是否可以向临时表添加一列,而无需创建新的列
让我们假设下表
Table1 | Table2 | Table3 |
Id Atype | Id Btype | Id Ctype |
1 A1 | 1 B1 | 1 C1 |
2 A2 | 2 B2 | 2 C2 |
3 A3 | 3 B3 | 3 C3 |
首先我想创建一个临时表:
CREATE TEMPORARY TABLE IF NOT EXISTS
temp_table ( INDEX(id) )
AS (
SELECT t1.id, t1.atype , t2.btype
FROM table1 t1
left join table2 t2 on t1.id = t2.id);
Result:
temp_table
Id Atype Btype
1 A1 B1
2 A2 B2
3 A3 B3
然后我想在临时表中添加 Ctype。 我怎样才能做到这一点? 我可以加入当前的临时表还是必须创建一个新的临时表?
我正在寻找的最终结果是 1 个临时表,如下所示:
Id Atype Btype Ctype
1 A1 B1 C1
2 A2 B2 C2
3 A3 B3 C3
最佳答案
您不能使用另一个加入
吗?
SELECT t1.id, t1.atype, t2.btype, t3.ctype
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.id = t2.id LEFT JOIN
table3 t3
ON t1.id = t3.id
如果您确实想修改现有表,则:
更改表 temp_table 添加 ctype varchar(255);
update temp_table tt join
table3 t3
on tt.id = t3.id
set tt.ctype = t3.ctype;
关于MySQL-从另一个表向临时表添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48948305/