I have follow database structure:
List item trips (ManyToMany to tags over table trips_tags) +----+----------------+ | id | name | +----+----------------+ | 1 | Czech Republic | | 2 | Germany | | 3 | Poland | +----+----------------+ tags +----+-----------+ | id | name | +----+-----------+ | 1 | Mountains | | 2 | Beach | | 3 | City | +----+-----------+ trips_tags +----------+---------+ | trips_id | tags_id | +----------+---------+ | 1 | 1 | | 1 | 2 | | 3 | 1 | +----------+---------+
I need to select trips which has all tags I specify.
- Need trips for tags Mountains as Beach I get only Czech Republic.
- Need trips only for tag Mountains, get Czech Republic and Poland
I wrote a simple SQL
SELECT trip.name, trip.id
FROM trips AS trip
WHERE (
SELECT COUNT(trip_tag.tags_id)
FROM trips_tags AS trip_tag
WHERE trip_tag.tags_id IN (1, 2)
AND trip_tag.trips_id = trip.id
) = numberOfTags`
Now I have a problem to write this SQL in DQL. Can anyone help?
Thank you