我想找出選擇什麼項目,什麼沒被選中成爲0入門選定項目,沒有選擇
這是列表框(或下拉菜單)是有用的,什麼是強調它的意思是它已經加入(LEFT JOIN)
我不能讓SQL工作:
SELECT StoreID, ft.id, name, count(1) as Selected
FROM `store_food_type` AS sft
LEFT JOIN food_type AS ft
ON ft.id = sft.food_type_id
WHERE StoreID = 17633
GROUP BY ft.id
UNION
SELECT 0, ft.id, name, 0 as Selected FROM food_type AS ft
編輯:它並沒有產生一個錯誤,它顯示的結果,但name
是重複的,我不想
它看起來是這樣的:
+------------+------+----------+----------+
| StoreID | id | name | Selected |
+------------+------+----------+----------+
| 17633 | 1 | Indian | 1 |
| 17633 | 8 | American | 1 |
| 0 | 1 | Indian | 0 |
| 0 | 2 | English | 0 |
| 0 | 25 | Kurdish | 0 |
| 0 | 5 | Chinese | 0 |
| 0 | 7 | Turkish | 0 |
| 0 | 8 | American | 0 |
+------------+------+----------+----------+
我想要的結果出現這樣的:因爲在你查詢UNION
的
+------------+------+----------+----------+
| StoreID | id | name | Selected |
+------------+------+----------+----------+
| 17633 | 1 | Indian | 1 |
| 17633 | 8 | American | 1 |
| 0 | 25 | Kurdish | 0 |
| 0 | 5 | Chinese | 0 |
| 0 | 7 | Turkish | 0 |
| 0 | 8 | American | 0 |
+------------+------+----------+----------+
此查詢以何種方式失效?它是否產生錯誤,是否運行沒有錯誤,但不選擇行或返回錯誤的行? – 2012-03-09 16:57:11
@MarkBannister我已經更新了我的問題。 – 2012-03-09 17:07:13
同一家商店可以多次選擇食物嗎? – Griffin 2012-03-09 17:57:54