首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

查询~该怎么处理

2012-02-27 
查询~~~~~~~~~~~~~~~~~~~表1idname12131422232431表2idage118318我想查出表1 查询条件 用表2的age列 查出

查询~~~~~~~~~~~~~~~~~~~
表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

热点排行