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

meadking 2009-09-07 08:35:23
表一:一般消费
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应用哦!
谢谢各位帮忙!我会另开帖子送分的哦
...全文
472 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
meadking 2009-09-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 wwwwa 的回复:]
别人的答复仔细看看,测试没有,有什么问题?
直接生成一个新表即可
[/Quote]
原来有个列不一样,要参数
到Access的查询分析器下面调试
比较好,哈哈
谢谢各位
wwwwb 2009-09-07
  • 打赏
  • 举报
回复
根据你上面的表达,应该是联合查询
select * from
(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)

这样不行?贴记录及要求结果出来看看
ACMAIN_CHM 2009-09-07
  • 打赏
  • 举报
回复

#2 楼是是已经给你SQL语句了吗? 为什么要用JOIN ?

你想实现的功能是什么?


建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
meadking 2009-09-07
  • 打赏
  • 举报
回复
怎么办?
用join吗?

表连接
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
a506690 2009-09-07
  • 打赏
  • 举报
回复
接分!LZ分真多,嘻嘻..
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
meadking 2009-09-07
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20090907/08/4162af31-c915-48fa-bdd9-be46ac300365.html
送分贴
WWWWA 2009-09-07
  • 打赏
  • 举报
回复
别人的答复仔细看看,测试没有,有什么问题?
直接生成一个新表即可
meadking 2009-09-07
  • 打赏
  • 举报
回复
或者用C#的dataTable,
table可以添加结果集吗?不行就一行一行datarow添加!!!!

内存表DataTable可以排序吗?这个我没有找到方法哦
meadking 2009-09-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 xblue3 的回复:]
表一:一般消费
id,主键
customer_id,客户id
cash,金额
type,消费类型:零食,公交费....
dt,消费时间

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

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

表四...................(很多这样的表,合并查询)
结果为:

单个客户的所有支出列表:

要求结果如下:这些记录都从不同表中获取的哦
(例子):

id,客户id,消费类型,金额,时间
1,cst_id01,孩子上学,2000元,9月1日
2,cst_id01,看望父母,300元,9月4日
3,cst_id01,房租物业,8000元,9月8日
.............................................................................................

[/Quote]

上面的表结构还不够清楚吗?
各位???
join方法怎么写?
还是用union

7,714

社区成员

发帖
与我相关
我的任务
社区描述
Microsoft Office Access是由微软发布的关系数据库管理系统。它结合了 MicrosoftJet Database Engine 和 图形用户界面两项特点。
社区管理员
  • Access
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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