SQL 多个 table 条件查询
SQL query from multiple table with condition
使用 MySQL,我想在一次插入中完成所有这些操作:
- table T1 contains column Ca & Cb. T1 is in database D1. Insert into table T1 specifying column Ca, Cb.
- table T2 contains column C2. T2 is in database D2. Set T1.Ca's value with T2.C2
- table T3 contains column C3 & C4. T3 is in database D2. use the T2.C2 value to query T3.C3 and use the C4 value of the same row to set T1.Cb
所以我做到了:
insert into T1(Ca, Cb)
select C2
from D2.T2
union
select C4
from D2.T3
where C3=T2.C2;
错误代码:1054。'where clause'
中的未知列 'T2.C2'
请帮忙。提前谢谢你。
合并的查询彼此独立。
你想加入这些表。
select C2,C4
from D2.T2 a
INNER JOIN D2.T3 b ON b.C3=a.C2;
如果我理解正确,你应该使用连接而不是联合
insert into D1.T1(Ca, Cb)
select T2.C2, T3.C4
from D2.T2
INNER JOIN D2.T3 on T2.C2 = T3.C3
工会的第二个select不知道第一个select的内容..所以你有错误
Unknown column 'T2.C2' in 'where clause'
使用 MySQL,我想在一次插入中完成所有这些操作:
- table T1 contains column Ca & Cb. T1 is in database D1. Insert into table T1 specifying column Ca, Cb.
- table T2 contains column C2. T2 is in database D2. Set T1.Ca's value with T2.C2
- table T3 contains column C3 & C4. T3 is in database D2. use the T2.C2 value to query T3.C3 and use the C4 value of the same row to set T1.Cb
所以我做到了:
insert into T1(Ca, Cb)
select C2
from D2.T2
union
select C4
from D2.T3
where C3=T2.C2;
错误代码:1054。'where clause'
中的未知列 'T2.C2'请帮忙。提前谢谢你。
合并的查询彼此独立。
你想加入这些表。
select C2,C4
from D2.T2 a
INNER JOIN D2.T3 b ON b.C3=a.C2;
如果我理解正确,你应该使用连接而不是联合
insert into D1.T1(Ca, Cb)
select T2.C2, T3.C4
from D2.T2
INNER JOIN D2.T3 on T2.C2 = T3.C3
工会的第二个select不知道第一个select的内容..所以你有错误
Unknown column 'T2.C2' in 'where clause'