[英]SQLite - How to select records from one table that are not in another table
[英]How do I select records from Sqlite table partially grouped?
我有一張這樣的桌子。 我想選擇基於group_id
分組的Fruits
,而不是Vegetables
和Nuts
。 對於Vegetable
和Nuts
,我全都想要。
group_id name type
-----------------------------------
1 Green Apple Fruit
1 Red Apple Fruit
1 Blue Apple Fruit
2 Green Peas Vegetable
2 Snow Peas Vegetable
2 Another Pea Vegetable
3 Ground Nut Nuts
3 Peanut Nuts
4 Carrot Vegetable
這就是我現在嘗試過的方式。 這很好用,但是我想知道是否有任何更簡單的方法。
select * from Grocessaries GROUP BY group_id HAVING type in ('Fruit', 'Drinks')
UNION all
select * from Grocessaries where type in ('Vegetable', 'Nuts')
基本上,我想要這樣的結果(將水果和所有蔬菜和堅果分組)
group_id name type
-----------------------------------
1 Green Apple Fruit
2 Green Peas Vegetable
2 Snow Peas Vegetable
2 Another Pea Vegetable
3 Ground Nut Nuts
3 Peanut Nuts
4 Carrot Vegetable
由於您是專門在UI中處理水果(和飲料)的,因此返回的實際名稱並不那么重要,因此您可以
SELECT group_id,
CASE type
WHEN 'Fruits' THEN 'Fruits' -- or whatever you want to display
WHEN 'Drinks' THEN 'Drinks'
ELSE name
END NameGrouped,
type
FROM Grocessaries
GROUP BY group_id, NameGrouped, type
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.