I have modeled an event to have multiple categories.
So I have an Event
table and an Event_Category
table. The Event_Category
table is obvious: it has two columns, the Event ID
and the Category name
.
The Categories are HIKE
, FAMILY
, CLASS_OR_LECTURE
, etc.
Maybe I'm having a brain fart but I can't for the life of me figure out the SQL query to get a list of events that are in a given list of Categories.
For example, I want a list of Events that have either of the Categories "HIKE" or "FAMILY".
Can anyone help, please?
select * from events where id in (select distinct id from events_categories where category_id in (1,2))
This isn't it?
SELECT DISTINCT event_id
FROM event_category
WHERE category_name IN ('HIKE', 'FAMILY', 'CLASS_OR_LECTURE')
If you want more than just the event id:
SELECT DISTINCT (whatever)
FROM event JOIN event_category USING (event_id)
WHERE category_name IN ('HIKE', 'FAMILY', 'CLASS_OR_LECTURE')
Select
e.*
From
events e
Where
Exists (
Select 'x'
From event_category ec
Where ec.event_id = e.event_id
And ec.category_name in ('HIKE', 'FAMILY')
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.