У меня есть такой стол. И я хочу выбрать Fruits
, сгруппированные вместе на основе group_id
, но не 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