将独立选择查询的结果合并到一个表

时间:2018-03-24 19:42:26

标签: mysql

我正在学习如何在MySQL中编写查询,我有一个包含交易的表格,希望获得总计1. revenues sold through the full life span2. Revenues in a day3. Revenues in a week和{{1 }}。所有这些都返回具有相同列的表

所以我有两个表,交易表保存我的所有交易和价格表,保存所有项目的价格,在购买物品时,我从价格表中获取itemid,以便我获取价格事务我需要加入这两个,这是我的表结构:

价格表

4. Revenues in a month

交易表

         |priceid | itemName | price |
         -----------------------------
         |1      |Bread     | 20    |

我知道如何使用3个查询获得一天,一周和一个月的总收入,收入,但想要一种方法在一个查询中获取所有3个值

我所知道的是

        |transactionid |pricefk |  transactiondate |
        --------------------------------------------
        |112           |1       |2018-03-22 19:15:05|       

获取所有终身收入

SELECT  SUM(p.price) as totalrevenue FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk

获得一天的收入

SELECT  SUM(p.price) as totalrevenue FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk  where t.transactiondate >= NOW() - INTERVAL 1 DAY

获得一周的收入

SELECT  SUM(p.price) as totalrevenue FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk  where YEARWEEK(t.transactiondate)=YEARWEEK(NOW())

获得一个月的收入

  

如何在一个查询中组合所有这些查询?

2 个答案:

答案 0 :(得分:0)

的QuickFix

您可以将它们包装在一个选择中,如下所示:

SELECT 
(SELECT SUM(p.price) FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk) 
as lifetimeRevenue,
(SELECT  SUM(p.price) FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk  where t.transactiondate >= NOW() - INTERVAL 1 DAY) 
as dayRevenue,
(SELECT  SUM(p.price) FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk  where YEARWEEK(t.transactiondate)=YEARWEEK(NOW())) 
as weekRevenue,
(SELECT  SUM(p.price) FROM transaction_table t JOIN price_tb p ON p.priceid = t.pricefk  WHERE MONTH(t.transactiondate) = MONTH(CURRENT_DATE()) AND YEAR(t.transactiondate) = YEAR(CURRENT_DATE())) 
as monthRevenue

在这里工作小提琴:http://www.sqlfiddle.com/#!9/70ef6/5

使其更简单

您没有使用任何外键,它们将有助于保持数据的完整性,并且通过使用它可以使查询变得更加简单。

您可以在transaction_table的创建表语句中添加类似的内容。

foreign key (pricefk) references price_tb(priceid)  

之后,在表的所有连接语句中,只包含表名:

SELECT 
(SELECT SUM(price) FROM transaction_table,price_tb)
as lifetimeRevenue,
(SELECT  SUM(price) FROM transaction_table,price_tb where transactiondate >= NOW() - INTERVAL 1 DAY) 
as dayRevenue,
(SELECT  SUM(price) FROM transaction_table,price_tb where YEARWEEK(transactiondate)=YEARWEEK(NOW())) 
as weekRevenue,
(SELECT  SUM(price) FROM transaction_table,price_tb WHERE MONTH(transactiondate) = MONTH(CURRENT_DATE()) AND YEAR(transactiondate) = YEAR(CURRENT_DATE())) 
as monthRevenue

完整的工作小提琴:http://www.sqlfiddle.com/#!9/9f333/6

(注意:我已将pricefk列重命名为priceid作为标准做法。)

答案 1 :(得分:0)

所有这些查询只返回一行,所以如果你需要在一行上得到结果,你可以尝试使用交叉连接

  select t1.totalrevenue_lifetime, t2.totalrevenue_day, t3.totalrevenue_week , t4.totalrevenue_month
  from (
      SELECT  SUM(p.price) as totalrevenue_lifetime 
      FROM transaction_table t 
      JOIN price_tb p ON p.priceid = t.pricefk
  ) t1 
  CROSS JOIN (
    SELECT  SUM(p.price) as totalrevenue_day 
    FROM transaction_table t 
    JOIN price_tb p ON p.priceid = t.pricefk  
    where t.transactiondate >= NOW() - INTERVAL 1 DAY

  ) t2 
  CROSS JOIN (
    SELECT  SUM(p.price) as totalrevenue_week 
    FROM transaction_table t 
    JOIN price_tb p ON p.priceid = t.pricefk  
    where YEARWEEK(t.transactiondate)=YEARWEEK(NOW())
  ) t3
  CROSS JOIN (
      SELECT  SUM(p.price) as totalrevenue_month 
      FROM transaction_table t 
      JOIN price_tb p ON p.priceid = t.pricefk  
      WHERE MONTH(t.transactiondate) = MONTH(CURRENT_DATE())
      AND YEAR(t.transactiondate) = YEAR(CURRENT_DATE())
  ) t4