MySQL使用CAST和SUM与JOIN

时间:2016-12-22 16:09:31

标签: mysql

我有以下3个MySQL队列:

-1 -

 SELECT CAST(SUM(revenue) AS CHAR) revenue FROM PA
 WHERE site_id = 2 AND data_date BETWEEN '2016-01-01' AND '2016-01-01';

-2 -

SELECT CAST(SUM(revenue) AS CHAR) revenue FROM PB
 WHERE site_id = 2 AND data_date BETWEEN '2016-01-01' AND '2016-01-01';

-3 -

SELECT CAST(SUM(revenue) AS CHAR) revenue FROM PC
 WHERE site_id = 2 AND data_date BETWEEN '2016-01-01' AND '2016-01-01';

每个人只会从查询表中获得收入。问题是我希望从所有3个表中获得总收入,任何想法我如何加入它们? 我的最后一次尝试是:

SELECT
    PA.CAST (SUM(PA.revenue) AS CHAR) revenue,
    PB.CAST (SUM(PB.revenue) AS CHAR) revenue,
    PC.CAST (SUM(PC.revenue) AS CHAR) revenue
FROM
    PA
INNER JOIN PB ON PA.site_id = PB.site_id
INNER JOIN PC ON PA.site_id = PC.site_id

但我的语法错误在这里有任何想法怎么做?

ERROR:

  

错误1064(42000):您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便使用接近' AS CHAR)的收入,          PB.CAST(SUM(PC'第1行

2 个答案:

答案 0 :(得分:0)

不要使用表别名来限定函数。

这是错误的......

  PA.CAST(SUM(revenue))
  ^^^

限定对列的引用...

  CAST(SUM(PA.revenue))
           ^^^

这就是语法错误。

但是我不认为你想加入这些表......那里有可能产生这些表的交叉产品(半笛卡尔积)。

如果没有从其中一个表返回任何行,则查询将返回零行。或者,如果从同一site_id的其中一个表中返回多行,则会生成“重复”行,SUM将会膨胀。

不知道你想要实现的目标......

给出工作查询的示例,我们可以提出这样的建议,尽管可能有更有效的方法来实现等效结果。

  SELECT SUM(v.revenue) AS revenue
    FROM ( 
           ( 
             SELECT SUM(PA.revenue) AS revenue
               FROM PA
              WHERE PA.site_id = 2 
                AND PA.data_date BETWEEN '2016-01-01' AND '2016-01-01'
           )
           UNION ALL
           (
             SELECT SUM(PB.revenue) AS revenue
               FROM PB
              WHERE PB.site_id = 2 
                AND PB.data_date BETWEEN '2016-01-01' AND '2016-01-01'
           )
           UNION ALL
           (
             SELECT SUM(PC.revenue) AS revenue
               FROM PC
              WHERE PC.site_id = 2 
                AND PC.data_date BETWEEN '2016-01-01' AND '2016-01-01'
           )
         ) v

答案 1 :(得分:0)

由于每个查询都会返回1行1列结果,因此您可以进行笛卡尔连接,即不加入。

SELECT a.revenue AS a_revenue
     , b.revenue AS b_revenue
     , c.revenue AS c_revenue
  FROM ( SELECT CAST(SUM(revenue) AS CHAR) revenue
           FROM PA
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
       ) a
     , ( SELECT CAST(SUM(revenue) AS CHAR) revenue
           FROM PB
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
       ) b
     , ( SELECT CAST(SUM(revenue) AS CHAR) revenue
           FROM PC
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
       ) c

如果您只想要总值而不是3个单独的值,请使用UNION ALL

SELECT CAST(SUM(revenue) AS CHAR) revenue
  FROM ( SELECT revenue
           FROM PA
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
         UNION ALL
         SELECT revenue
           FROM PB
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
         UNION ALL
         SELECT revenue
           FROM PC
          WHERE site_id = 2
            AND data_date BETWEEN '2016-01-01' AND '2016-01-01'
       ) x