求sql语句

yuxi5622 2011-06-16 03:12:15
有表1如下:
ID NAME
1 A
2 B
3 C
表2如下:
ID USER
1 01
1 02
1 01
2 02
2 03
3 02
我想得到以下的结果:
ID NAME USER
1 A 01
1 A 02
2 B 02
2 B 03
3 C 02
应该怎么写这个语句呢???
...全文
65 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-06-16
  • 打赏
  • 举报
回复
select
distinct a.id,a.name,b.user
from
a, b
where
a.id=b.id
大力水手 2011-06-16
  • 打赏
  • 举报
回复

select distinct a.id,a.name,b.user
from 表1 a,表2 b where a.id=b.id
叶子 2011-06-16
  • 打赏
  • 举报
回复

declare @表1 table (ID int,NAME varchar(1))
insert into @表1
select 1,'A' union all
select 2,'B' union all
select 3,'C'

declare @表2 table (ID int,[USER] varchar(2))
insert into @表2
select 1,'01' union all
select 1,'02' union all
select 1,'01' union all
select 2,'02' union all
select 2,'03' union all
select 3,'02'

select distinct a.*,b.NAME from @表2 a
left join @表1 b on a.ID=b.ID
/*
ID USER NAME
----------- ---- ----
1 01 A
1 02 A
2 02 B
2 03 B
3 02 C
*/
chuanzhang5687 2011-06-16
  • 打赏
  • 举报
回复
select distinct tb1.id , tb1.name ,tb2.users
from tb1 left join tb2
on tb1.id =tb2.id
挨踢直男 2011-06-16
  • 打赏
  • 举报
回复
select a.id,a.name,t.user from
(
select distinct * from b
) as t join a on t.id = a.id
GoAwayZ 2011-06-16
  • 打赏
  • 举报
回复
select distinct a.id,a.name,b.user
from 表1 a,表2 b where a.id=b.id

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧