-1

MYSQL(版本5.1)中以下查询的GOT错误

SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1,fact_2,0 as fact_3,0 as fact_4 from table_1
intersect
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as combined_table
group by month,year

带有代码#1064的错误行:-

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法,以便在第 5 行的“select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2 ) as ct g 附近使用

但以下查询给出了所需的结果:-

SELECT year,month,sum(fact_1),sum(fact_2),sum(fact_3),sum(fact_4)
from(
select year,month,fact_1 ,fact_2,0 as fact_3,0 as fact_4 from table_1
union
select year,month,0 as fact_1,0 as fact_2,fact_3,fact_4 from table_2
) as ct
group by month,year

谁能告诉我犯了什么错误?任何人都可以帮助我了解问题背后的根本原因。

4

2 回答 2

5

INTERSECT您可以使用INNER(self)轻松伪造JOIN,这样您只能从两个结果集中获取行:

    SELECT `a`.`id`, `a`.`name`
      FROM `a`
INNER JOIN `b`
     USING (`id`, `name`)

MINUS可以用 a 伪造LEFT JOIN

    SELECT DISTINCT `a`.`id`, `a`.`name`
      FROM `a`
 LEFT JOIN `b`
     USING (`id`, `name`)
     WHERE `b`.`id` IS NULL
于 2009-09-03T09:35:27.220 回答
0

MySQL 不支持 INTERSECT 关键字。SELECT for 5.1 的完整语法在这里:

http://dev.mysql.com/doc/refman/5.1/en/select.html

于 2009-09-03T09:25:37.493 回答