任何人都可以建议“相交”的替代方案吗? & “减”对于MYSQL?

发布于 2024-08-03 16:27:25 字数 844 浏览 5 评论 0原文

在 MYSQL(version5.1) 错误行中出现以下查询错误,

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 服务器版本 在“选择”附近使用正确的语法 年、月、0 作为 fact_1,0 作为 表_2 中的事实_2、事实_3、事实_4 )为 ct g' 位于第 5 行

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

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

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

GOT error for the following query in MYSQL(version5.1)

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

Error Line with code#1064:-

You have an error in your SQL syntax;
check the manual that corresponds to
your MySQL server version for the
right syntax to use near 'select
year,month,0 as fact_1,0 as
fact_2,fact_3,fact_4 from table_2 ) as
ct g' at line 5

but following query was giving desired Result:-

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

Can anybody tell what error i am committing?
can Anybody help me to understand the root cause behind the Problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难理解 2024-08-10 16:27:25

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

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

MINUS 可以用 LEFT JOIN 来伪造:

    SELECT DISTINCT `a`.`id`, `a`.`name`
      FROM `a`
 LEFT JOIN `b`
     USING (`id`, `name`)
     WHERE `b`.`id` IS NULL

you can fake INTERSECT quite easily using an INNER (self) JOIN, this way you’ll only get rows from both resultsets:

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

MINUS can be faked with a LEFT JOIN:

    SELECT DISTINCT `a`.`id`, `a`.`name`
      FROM `a`
 LEFT JOIN `b`
     USING (`id`, `name`)
     WHERE `b`.`id` IS NULL
匿名的好友 2024-08-10 16:27:25

MySQL 不支持 INTERSECT 关键字。 5.1 的 SELECT 完整语法如下:

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

MySQL doesn't support the INTERSECT keyword. Full syntax of SELECT for 5.1 is here:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文