使用MS SQL SERVER 2008
我有以下情况:
===================
| Table A |
===================
|id |desc |code
| 1 |ballpen |1010
| 2 |pencil |1010
| 3 |stabilo |1010
| 4 |pins |1011
| 5 |clips |1011
===================
| Table B |
===================
| id |code
| 1010 |AAA
| 1011 |BBB
| 1013 |CCC
我需要完成的事情如下:
===================
| Table C |
===================
|id |desc |code |code 2
| 1 |ballpen |1010 |AAA
| 2 |pencil |1010 |AAA
| 3 |stabilo |1010 |AAA
| 4 |pins |1011 |BBB
| 5 |clips |1011 |BBB
这是我到目前为止的情况,不幸的是LEFT JOIN
导致最终查询输出膨胀
select *
from table a
left join table b on a.code = b.id
结果:
========
Table D
========
|id |desc |code |code2
|1 |ballpen |1010 |AAA
|1 |ballpen |1010 |AAA
|1 |ballpen |1010 |AAA
|2 |pencil |1010 |AAA
|2 |pencil |1010 |AAA
|2 |pencil |1010 |AAA
|3 |stabilo |1010 |AAA
|3 |stabilo |1010 |AAA
|3 |stabilo |1010 |AAA
|4 |pins |1011 |BBB
|4 |pins |1011 |BBB
|5 |clips |1011 |BBB
|5 |clips |1011 |BBB
答案 0 :(得分:0)
注意:我认为您在join.let中使用了相同的表名,请尝试使用不同的表名
我使用了Left Join它有效..
create table #master
(
ID int,
descs varchar(15),
code int
)
create table #codes
(
ids int,
codes varchar(15)
)
insert into #master values(1,'ballpen',1010)
insert into #master values(2,'pencil',1010)
insert into #master values(3,'stabilo',1010)
insert into #master values(4,'pins',1011)
insert into #master values(5,'Clips',1011)
insert into #codes values(1010,'AAA')
insert into #codes values(1011,'BBB')
insert into #codes values(1013,'CCC')
select ID,descs,code,codes from #master a
left join #codes b
on a.code = b.ids
输出:
ID descs code codes
1 ballpen 1010 AAA
2 pencil 1010 AAA
3 stabilo 1010 AAA
4 pins 1011 BBB
5 Clips 1011 BBB
如果左连接不起作用,请使用内连接:
select ID,descs,code,codes from #master a,#codes b
where a.code=b.ids
答案 1 :(得分:0)
你应该尝试内部加入 的 的
的 SELECT
distinct a.id,a.desc1,a.code,b.code as code2
from a
inner join b
on a.code = b.id
的
答案 2 :(得分:0)
我想Plus.PeopleApi.loadVisible(mGoogleApiClient, null)
.setResultCallback(this);
不包含唯一的table B
,因此您需要对所选数据集code
进行操作
这里是测试:
distinct
下面的结果截图: