表1
id name
1 2
1 3
1 4
2 2
2 3
2 4
3 1
表2
id age
1 18
3 18
我想查出表1 查询条件 用表2的age列 查出表1(age = 18)
查询结果 表1
id name
1 2
1 3
1 3
1 4
3 1
记得有什么 内练 外联 的 我不会 还有什么多对一 一对多的
------解决方案--------------------------------------------------------
一边看自己一边可以尝试
内联还好,外联老手都经常忘
------解决方案--------------------------------------------------------
select a.id, a.name, b.age
from 表1 a
inner join 表2 b on a.id=b.id;
------解决方案--------------------------------------------------------
SELECT ID, NAME FROM Table1 WHERE ID=(SELECT ID FROM Table2 WHERE AGE=18)
------解决方案--------------------------------------------------------
- SQL code
declare @表1 table (id int,name int)insert into @表1select 1,2 union allselect 1,3 union allselect 1,4 union allselect 2,2 union allselect 2,3 union allselect 2,4 union allselect 3,1select * from @表1declare @表2 table (id int,age int)insert into @表2select 1,18 union allselect 3,18select * from @表2select a.* from @表1 a right join @表2 b on a.id=b.id
------解决方案--------------------------------------------------------
- SQL code
select 表1.* from 表1 inner join 表2 on 表1.id=表2.id and 表2.age=18
------解决方案--------------------------------------------------------
楼主要两个1,3,因此需要左连接left join
------解决方案--------------------------------------------------------
- SQL code
SELECT * FROM 表1 where id in ( select id from 表2 where age ='18')
------解决方案--------------------------------------------------------
结贴吧
------解决方案--------------------------------------------------------
select 表1.id as id,name
from 表1 left outer join
(select id,age
from 表2
where age='条件') as 表3
on 表1.id = 表3.id