我有一个 MySQL 查询,我想稍微优化一下。这里是:
SELECT `items`.category AS 'Category', 'Without' AS 'Which', COUNT(*) AS 'Count'
FROM `items`
WHERE `id`=706
GROUP BY `category`
UNION
SELECT `items`.category AS 'Category', 'With' AS 'Which', COUNT(*) AS 'Count'
FROM `items`
WHERE `id`=706 AND `qa_items`.pointsoff=0
GROUP BY `category`;
我得到的是:
+---------------------------+---------+-------+
| Category | Which | Count |
+---------------------------+---------+-------+
| Category A | Without | 3 |
| Category B | Without | 8 |
| Category C | Without | 4 |
| Category A | With | 2 |
| Category B | With | 6 |
| Category C | With | 4 |
+---------------------------+---------+-------+
但我想要的是:
+---------------------------+---------+-------+
| Category | Without | With |
+---------------------------+---------+-------+
| Category A | 3 | 2 |
| Category B | 8 | 6 |
| Category C | 4 | 4 |
+---------------------------+---------+-------+
我知道我可能必须做某种子查询,但我不确定如何引用我在子查询中寻找的类别。我确定这里缺少一些简单的东西。任何人都可以帮忙吗?谢谢!