sql查询在多对多关系中返回单个记录

时间:2016-06-29 22:43:14

标签: jquery mysql sql many-to-many

如何获得project_id,其中一行有tech_id = 1和technology_id = 2?

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

1 个答案:

答案 0 :(得分:1)

product_id分组,只选择那些同时拥有technology_id s

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