如何实现这样的表的关联效果

TienBao 2006-11-22 11:10:08
一个表中,有一个字段表示类别 如type
id type
1 2
2 2
3 2
4 3
4 3
想要的效果是:
id type id2 type2
1 2 3 3
2 2 4 3
3 2 null null

如果type =3 的比 type=2的多 type=2的那些数据就显示null
...全文
220 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
TienBao 2006-11-22
  • 打赏
  • 举报
回复
标题写的不好,总是没人回,呵呵。
TienBao 2006-11-22
  • 打赏
  • 举报
回复
一个mytable表中,有一个字段表示类别 如type
id type
1  a
2  a
3  a
4  b
4  b
想要的效果是:
id type id2 type2
1  a  3  b
2  a  4  b
3  a  null null

如果type =b 的比 type=a的多 type=a的那些数据就显示null

就是把表平移了。
如何实现?
hhhdyj 2006-11-22
  • 打赏
  • 举报
回复
没看明白
TienBao 2006-11-22
  • 打赏
  • 举报
回复
自己顶一下。
louifox 2006-11-22
  • 打赏
  • 举报
回复
declare @t table(id int, type int)
insert @t select 1, 2
union all select
2 , 2
union all select
3 , 2
union all select
4 , 3
union all select
4 , 3
declare @t1 table(s int identity(1,1),id int, type int)
declare @t2 table(s int identity(1,1),id int, type int)
insert @t1 select * from @t where type=2
insert @t2 select * from @t where type=3

select t1.id,t1.type,t2.id as id2,t2.type as type2
from @t1 t1 left outer join @t2 t2 on t1.s=t2.s
(所影响的行数为 2 行)

id type id2 type2
----------- ----------- ----------- -----------
1 2 4 3
2 2 4 3
3 2 NULL NULL

(所影响的行数为 3 行)

34,588

社区成员

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

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