| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1699 人关注过本帖
标题:2个表怎么知道通过哪2个字段关联
只看楼主 加入收藏
sheng000
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-10-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
2个表怎么知道通过哪2个字段关联
2个表怎么知道通过哪2个字段关联
2016-10-20 11:17
mywisdom88
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:191
帖 子:3146
专家分:8408
注 册:2015-3-25
收藏
得分:0 
--测试数据
create table #tb1(id int,name varchar(10))
create table #tb2(id int,sex varchar(4),age int)
insert into #tb1
select 1001,'张三' union all
select 1002,'李四' union all
select 1003,'王五' union all
select 1004,'赵六'

insert into #tb2
select 1001,'男',21 union all
select 1002,'女',30 union all
select 1003,'男',41 union all
select 1004,'男',60

--方法1,用where 关联,找出所有男的
select a.id,a.name,b.sex,b.age from #tb1 a,#tb2 b where a.id=b.id and b.sex='男'
--方法2,用where 关联,找出所有男的,年龄小于41的
select a.id,a.name,b.sex,b.age from #tb1 a,#tb2 b where a.id=b.id and b.sex='男' and b.age < 41
--方法3,用 left jon ,inner join 等等内关联,找出所有,年龄小于41的
select a.id,a.name,b.sex,b.age from #tb1 a
inner join #tb2 b on a.id=b.id and b.age<41

--你可以去百度查 left join ,inner join,right join 的区别
2016-10-20 13:44
快速回复:2个表怎么知道通过哪2个字段关联
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015278 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved