查询多个表(字段差不多相同)合并结果,并按一个时间字段排序

meadking 2009-09-07 08:37:44
表一:一般消费
id,主键
customer_id,客户id
cash,金额
type,消费类型:零食,公交费....
dt,消费时间

表二:日常开支
id,主键
customer_id,客户id
cash,金额
type,消费类型:水电,房租....
dt,缴费时间

表二:其它支出
id,主键
customer_id,客户id
cash,金额
type,消费类型:医药看病,保险,上学费用,
dt,缴费时间

表三...................很多这样的表,合并查询,结果为:
单个客户的所有支出列表:
select * from table1,table2 where customer_id=单个客户id order by dt时间

(例子)最后的结果集像下面一样:

id,客户id,消费类型,金额,时间
1,cst_id01,孩子上学,2000元,9月1日
2,cst_id01,看望父母,300元,9月4日 (备注:礼品)
3,cst_id01,房租物业,8000元,9月8日(一年费用)
.............................................................................................

这是个例子,表1,表2不能合并,因为有很多客户,方便总共的统计!

我用union all 只能查询两个表
三个表就错了......
我的应用是:C#+access,单机WinForm应用
一定要注意,不是web应用哦!
谢谢各位帮忙!我会另开帖子送分的哦
...全文
295 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
meadking 2009-09-08
  • 打赏
  • 举报
回复
原来有个列不一样,要参数
到Access的查询分析器下面调试
比较好,哈哈
谢谢各位
SQL77 2009-09-07
  • 打赏
  • 举报
回复
SELECT * FROM 
(select customer_id,cash,type,dt from table1
union all
select customer_id,cash,type,dt from table2)AS T


union all

select customer_id,cash,type,dt from table3

order by dt
????
mbh0210 2009-09-07
  • 打赏
  • 举报
回复
建议搞点数据出来,以及你想要的结果出来
viki117 2009-09-07
  • 打赏
  • 举报
回复
用括号。。把前2个合并表另命名下。。然后继续用union
meadking 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wwwwb 的回复:]
我用union all 只能查询两个表
三个表就错了......
提示什么错误信息?

select customer_id,cash,type,dt from t1
union all
select customer_id,cash,type,dt from t2
union all
select customer_id,cash,type,dt from t3
order by dt
[/Quote]

我也是遇到这个问题啊,三个表就出错了啊???????????
C#中用的access
wwwwb 2009-09-07
  • 打赏
  • 举报
回复
我用union all 只能查询两个表
三个表就错了......
提示什么错误信息?

select customer_id,cash,type,dt from t1
union all
select customer_id,cash,type,dt from t2
union all
select customer_id,cash,type,dt from t3
order by dt
cnliming 2009-09-07
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090907/08/9c973423-6327-45a8-95a4-c1abe9db5699.html
haming 2009-09-07
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090907/08/9c973423-6327-45a8-95a4-c1abe9db5699.html
SQL77 2009-09-07
  • 打赏
  • 举报
回复
弄点数据更清楚,不难
a506690 2009-09-07
  • 打赏
  • 举报
回复
这是个例子,表1,表2不能合并,因为有很多客户,方便总共的统计!




啥意思啊,没看明白,顶一下!
dllm2008 2009-09-07
  • 打赏
  • 举报
回复
select * from table1,table2 where table1.customer_id=单个客户id and table1.customer_id = table2.customer_id and table2.customer_id=table3.customer_id order by table1.dt时间
ACMAIN_CHM 2009-09-07
  • 打赏
  • 举报
回复
select customer_id,cash,type,dt from table1
union all
select customer_id,cash,type,dt from table2
union all
select customer_id,cash,type,dt from table3
order by dt
feidongai 2009-09-07
  • 打赏
  • 举报
回复
路过标记一下并学习
chinawei0710 2009-09-07
  • 打赏
  • 举报
回复
来要分的。。
marey_marey111 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 marey_marey111 的回复:]
select * from (select * from table1 where customer_id=单个客户id
union select * from table2 where customer_id=单个客户id union select * from table3 where customer_id=单个客户id) order dt
[/Quote]


别人给你写了SQL。。那没有什么意思。。提供一个思路。。。才能学到东西~~~~
netstray 2009-09-07
  • 打赏
  • 举报
回复
路过标记一下并学习
wh_wangjun 2009-09-07
  • 打赏
  • 举报
回复
在access中建个视图,合并你所需要的表。
这样查询方便一些。
meadking 2009-09-07
  • 打赏
  • 举报
回复
我要具体的sql
呵呵
marey_marey111 2009-09-07
  • 打赏
  • 举报
回复
select * from (select * from table1 where customer_id=单个客户id
union select * from table2 where customer_id=单个客户id union select * from table3 where customer_id=单个客户id) order dt
marey_marey111 2009-09-07
  • 打赏
  • 举报
回复
union,1,2,3表呗。。然后按你想要的结果排序。。
加载更多回复(3)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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