gpt4 book ai didi

mysql - 我在查询 SQL (UNION ALL) 中哪里出错了?

转载 作者:行者123 更新时间:2023-11-29 18:03:43 25 4
gpt4 key购买 nike

我尝试使用UNION ALL,但出现错误:

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 ') GROUP BY Product_id' at line 13

如何为 UNION 表和结果 GROUP BY 设置正确的别名?

示例:sqlfiddle

最佳答案

这是正确的查询结构,UNION/ALL 不需要别名,但派生表需要。

SELECT
Product_id,
sum(Total) as sumOfTotal
FROM ((SELECT
b.Products_idProducts AS Product_id,
COUNT(b.Products_idProducts) AS Total
FROM products_has_colors a
RIGHT JOIN products_has_colors b
ON a.Colors_idColors = b.Colors_idColors
AND b.Products_idProducts <> a.Products_idProducts
WHERE a.Products_idProducts = 1
GROUP BY b.Products_idProducts)

UNION ALL

(SELECT
b.Products_idProducts AS Product_id,
COUNT(b.Products_idProducts) AS Total
FROM Products_has_ProductComposition a
RIGHT JOIN Products_has_ProductComposition b
ON (a.CompositionId = b.CompositionId
AND b.Products_idProducts <> a.Products_idProducts)
WHERE a.Products_idProducts = 1
GROUP BY b.Products_idProducts)) mytable
GROUP BY Product_id;

关于mysql - 我在查询 SQL (UNION ALL) 中哪里出错了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48102462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com