作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用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/
我是一名优秀的程序员,十分优秀!