我有一个具有以下结构的数据库。我的机构表中的每一行都有一个保存在单独表格中的每日信息。我们想要的是如何查询数据库,以便我们获得按_info
表中相应天数分组的每个机构信息表的价格列总和。我的数据库是MySQL。
表institutions
:
id | institution_name | institution_info_table_name
-----------------------------------------------------
1 | "A" | "A_info"
-----------------------------------------------------
2 | "B" | "B_info"
表A_info
:
id | date | price
-----------------------
1 | 2013/02/12 | 12
-----------------------
2 | 2013/02/13 | 20
表B_info
:
id | date | price
-----------------------
1 | 2013/02/12 | 30
-----------------------
2 | 2013/02/13 | 50
输出:
date | price
------------------
2013/02/12 | 42
------------------
2013/02/13 | 70
编辑:
我尝试了以下查询,但它无效:
"select sum( A_info.price) ,A_info.date from A_info,B_info group by date"
答案 0 :(得分:1)
这很简单,试试
select
x.date,
sum(x.price) as price
from
(
(
select
date,
sum(price) as price
from A_info
group by date
)
union all
(
select
date,
sum(price) as price
from B_info
group by date
)
)x
group by x.date
答案 1 :(得分:0)
您是否尝试过此基本查询?
select A_info.price+B_info.price from A_info, B_info where A_info.id=B_info.id and
A_info.data=B_info.date;