这样的2表行列转换关联查询怎么写?

FFSB 2013-03-20 10:55:12
表A
id index1 index2 index3
3 1 2 3
2 1 1 3
5 2 3 3
4 3 1 5

表B
index data
1 aa
2 ccc
3 ddd
5 bb
6 aaa
4 eee

如要查询id=2,希望查询结果(带排序编号)
order id index data
1 2 1 aa
2 2 1 aa
3 2 3 ddd
这样的关联查询SQL怎么写?
...全文
170 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
FFSB 2013-03-20
  • 打赏
  • 举报
回复
[quote=引用 1 楼 wyj23114 的回复:] SQL code ... quote] order是用的index1,index2,index3的1,2,3不是随机的
伤痕累累 2013-03-20
  • 打赏
  • 举报
回复
select [order]=row_number()over(order by getdate()),id,t.[index],data from tbb join (
select id,[index]=index1 from tba where id=2
union all
select id,[index]=index2 from tba where id=2
union all
select id,[index]=index3 from tba where id=2
)t on tbb.[index]=t.[index]
习惯性蹭分 2013-03-20
  • 打赏
  • 举报
回复
1楼的应该可以满足你的要求了,他的事order也不是随机的,是取行序号。你要不把order by getdate()改成 order by data

34,838

社区成员

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

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