如何查询多张表内容,并排序

leo_dark 2011-02-10 02:26:55
举例


如表A中有列
A_Name A_AddTime

值分别是
A一 2011-01-01
A四 2011-01-04



如表B中有列
B_Name A_AddTime

值分别是
B二 2011-01-02
B五 2011-01-05


如表C中有列
C_Name A_AddTime

值分别是
C三 2011-01-03
C六 2011-01-06




我要实现的效果为

A一 2011-01-01
B二 2011-01-02
C三 2011-01-03
A四 2011-01-04
B五 2011-01-05
C六 2011-01-06



SQL语句该如何去写? 麻烦指教
...全文
33 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
leo_dark 2011-02-10
谢谢 各位了 结贴了。
嘿嘿 见笑了 对SQL语句掌握的不好。。
回复
leo_dark 2011-02-10


[Quote=引用 4 楼 roy_88 的回复:]
用1樓方法,把結果集連起來再排序
[/Quote]


方便给个语句吗?
回复
快溜 2011-02-10
select * from
(select A_Name as name,A_AddTime as date,'A表' as 表名 from A
union all
select B_Name as name,B_AddTime as date,'B表' as 表名 from B
union all
select C_Name as name,C_AddTime as date,'C表' as 表名 from C
) aa order by date
回复
中国风 2011-02-10
用1樓方法,把結果集連起來再排序
回复
leo_dark 2011-02-10
[Quote=引用 1 楼 ssp2009 的回复:]
SQL code
select * from
(select A_Name as name,A_AddTime as date from A
union all
select B_Name as name,B_AddTime as date from C
union all
select C_Name as name,C_AddTime as date from C
) a……
[/Quote]



不好意思 麻烦你了,我还有一个问题

如果我想显示加一列 显示出来自于什么表
比如说 表A 就叫A表
表B 就叫B表
A一 2011-01-01 A表
B二 2011-01-02 B表
C三 2011-01-03 C表
A四 2011-01-04 A表
B五 2011-01-05 B表
C六 2011-01-06 C表


可以吗?如何显示呢?
回复
leo_dark 2011-02-10
[Quote=引用 1 楼 ssp2009 的回复:]
SQL code
select * from
(select A_Name as name,A_AddTime as date from A
union all
select B_Name as name,B_AddTime as date from C
union all
select C_Name as name,C_AddTime as date from C
) a……
[/Quote]


请问 如果有更多列呢?比如说20列

那样子的话有没有更简便的方法呢?
回复
快溜 2011-02-10
select * from 
(select A_Name as name,A_AddTime as date from A
union all
select B_Name as name,B_AddTime as date from C
union all
select C_Name as name,C_AddTime as date from C
) aa order by date
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2011-02-10 02:26
社区公告
暂无公告