jhondoe500 jhondoe500 - 5 months ago 9
SQL Question

sql query in many to many relation returns single record

How can I get project_id, which has technology_id = 1 and technology_id = 2 in one row?

+------------+---------------+
| project_id | technology_id |
+------------+---------------+
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 7 | 1 |
| 9 | 1 |
+------------+---------------+

Answer

Group by the product_id and take only those groups having both technology_ids

select project_id
from your_table
where technology_id in (1,2)
group by project_id
having count(*) = 2