假设我有两个表TableA和TableB,其结构如下所示:-
**TableA**
id | col_1 | col_2
1 | A | B
2 | C | D
3 | E | F
4 | G | H
**TableB**
id | TableA_first_col_id | TableA_second_col_id
1 | 1 | 2
2 | 1 | 3
现在,我想检查TableB TableA_first_col_id列或TableA_second_col_id列中是否存在TableA id。 结果应该是
**TableA.id**
1
2
3
如何为此编写优化的mysql查询?
答案 0 :(得分:0)
select *
from table1 t1
where not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.program = t2.program
)
答案 1 :(得分:0)
尝试以下;
Select A.id
From TableA A
Where A.id in (select TableA_first_col_id From TableB) OR
A.id in (select TableA_second_col_id from TableB)
答案 2 :(得分:0)
select id from tableA where exists
( select id
from tableB
where tableA.id = tableB.first_col_id or tableA.id = tableB.second_col_id
)