简体   繁体   English

MySQL使用CAST和SUM以及JOIN

[英]MySQL using CAST and SUM with JOIN

I have the following 3 MySQL queires: 我有以下3个MySQL查询器:

-1- -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- -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- -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';

each one will give me the revenue only from the queried table. 每个人只会从查询的表中给我收入。 Problem is i want to get total revenue from all 3 tables, any idea how can i join them? 问题是我想从所有3张桌子中获得总收入,我知道如何加入它们? my last attempt was: 我最后的尝试是:

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

but i have error in my syntax here...any idea how to do it? 但是我在这里的语法有错误...任何想法怎么做?

ERROR: 错误:

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS CHAR) revenue, PB.CAST(SUM(PC' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在“ AS CHAR”收入PB.CAST(SUM(PC')在第1行附近使用

Don't qualify the function with a table alias. 不要用表别名限定函数。

This is wrong... 错了

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

Qualify the reference to the column... 限定对列的引用...

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

That's the syntax error. 那是语法错误。

But I don't think you want to join those tables... there's potential there for producing a cross product (semi-Cartesian product) of those tables. 但是我不认为您想加入这些表...那里存在产生这些表的叉积(半笛卡尔积)的潜力。

If no rows are returned from one of the tables, then the query will return zero rows. 如果其中一个表未返回任何行,则查询将返回零行。 Or, if more than one row is returned from one of the tables for the same site_id , that's going to produce "duplicate" rows, and the SUM will be inflated. 或者,如果从一个同一个site_id的表中返回一个以上的行,这将产生“重复的”行,并且SUM将被夸大。


Without knowing what you are attempting to achieve... 不知道您要达到的目标...

Given examples of working queries, we can propose something like this, though there may be a more efficient way to achieve an equivalent result. 给定工作查询的示例,我们可以提出类似的建议,尽管可能有一种更有效的方法来获得同等的结果。

  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

Since each query will return a 1-row, 1-column result, you can just do a Cartesian join, ie don't join. 由于每个查询都将返回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

If you just want the total value, rather than the 3 individual values, use UNION ALL : 如果您只想要总值,而不是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM