如果 ID 在2个表中都是主键,应该建立连接:select tab1.id from tab1 join tab2 on tab1.id=tab2.id where tab1.age>20 or tab2.sex in 'man' 或select tab1.id from tab1, tab2 where tab1.id=tab2.id and (tab1.age>20 or tab2.sex in 'man')
select tab1.id from tab1 inner join tab2 on tab1.id=tab2.id where tab1.age>20 or tab2.sex in 'man'
select distinct tab1.id from tab1,tab2 where tab1.age>20 or tab2.sex in 'man'
我也提供一种写法吧: select distinct id from ((select id from tab1 where age>20) union (select id from tab2 where sex in 'man')) as tab
select distinct tab1.id from tab1,tab2 where tab1.id=tab2.id and (tab1.age>20 or tab2.sex in 'man')
Slect Distinct id From tab1 Where tab1.age > 20 Union Select Distinct id From tab2 Where tab2.sex='man'
where tab1.age>20 or tab2.sex in 'man'
select distinct id from ((select id from tab1 where age>20) union (select id from tab2 where sex in 'man')) as tab
Union
Select Distinct id From tab2 Where tab2.sex='man'