SELECT DISTINCT companies id companies name COUNT comp_tags tid as FRO

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
SELECT
DISTINCT
companies.id,
companies.name,
COUNT(comp_tags.tid) as q
FROM
cities,
city_delivery,
districts,
companies,
comp_tags
WHERE
cities.id IN (9)
AND
cities.id = districts.cid
AND
districts.id = city_delivery.district
AND
city_delivery.cid = companies.id
AND
companies.id = comp_tags.cid
AND
comp_tags.tid IN (17,18,19)
GROUP BY
comp_tags.cid,
comp_tags.tid
HAVING
COUNT(comp_tags.tid) = 3