我有两个包含以下示例数据的表:
Table 1: `item_name`
| item_id | item_desc |
| 1 | apple |
| 2 | orange |
| 3 | banana |
| 4 | grape |
| 5 | mango |
Table 2: `user_items`
| user_id | item_id |
| 127 | 1 |
| 127 | 2 |
| 127 | 4 |
| 128 | 1 |
| 128 | 5 |
我正在尝试使用以下查询选择user_id 127和128中的每个item_id以及相应的item_desc:
SELECT IFNULL(COUNT(ui.user_id),0) AS total,in.item_desc
FROM user_items AS ui
RIGHT OUTER JOIN item_name AS in
ON ui.item_id = in.item_id
WHERE ui.user_id IN (127,128)
GROUP BY ui.item_id
ORDER BY total DESC
上述查询的结果是:
| total | item_desc |
| 2 | apple |
| 1 | orange |
| 1 | grape |
| 1 | mango |
但是它没有包含item_id 3,banana,我想用RIGHT OUTER JOIN检索它.我希望得到一个看起来像这样的结果:
| total | item_desc |
| 2 | apple |
| 1 | orange |
| 1 | grape |
| 1 | mango |
| 0 | banana |
最佳答案
原文链接:https://www.f2er.com/mysql/433402.html