I have two tables - incoming tours(id,name)andincoming_tours_cities(id_parrent, id_city)
idin the first table is unique, and for each unique row from the first table there is a list id_city- s in the second table (i.e. id_parrentin the second table it is equal idfrom the first table)
for instance
incoming_tours
|--id--|------name-----|
|---1--|---first_tour--|
|---2--|--second_tour--|
|---3--|--thirth_tour--|
|---4--|--hourth_tour--|
incoming_tours_cities
|-id_parrent-|-id_city-|
|------1-----|---4-----|
|------1-----|---5-----|
|------1-----|---27----|
|------1-----|---74----|
|------2-----|---1-----|
|------2-----|---5-----|
........................
This means that it first_tourhas a list of cities -("4","5","27","74")
And second_tourthere is a list of cities -("1","5")
Suppose I have two values - 4and 74:
Now I need to get all the rows from the first table, where the values are both listed in the list of cities. that is, it should return only first_tour (because 4 and 74 are in this list of cities)
So I wrote the following query
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
And it works great.
, ( 15), .
.
SELECT t.name
FROM `incoming_tours` t
JOIN `incoming_tours_cities` tc0 ON tc0.id_parrent = t.id
AND tc0.id_city = '4'
JOIN `incoming_tours_cities` tc1 ON tc1.id_parrent = t.id
AND tc1.id_city = '74'
.........................................................
JOIN `incoming_tours_cities` tc15 ON tc15.id_parrent = t.id
AND tc15.id_city = 'some_value'
45s ( , )
, ?