选择具有多个属性和值的ID

时间:2016-09-07 15:46:35

标签: mysql

我有table1:

+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  2 | name2 |
|  3 | name3 |
|  4 | name4 |
+----+-------+

我有table2:

+ ------ + --------- + ---------- +
| id | attribut |价值|
+ ------ + --------- + ---------- +
| 1 | 1 | 1 |
| 1 | 3 | 3 |
| 2 | 1 | 1 |
| 2 | 3 | 4 |
+ ------ + --------- + --------- +

我想在table1中选择表2中具有(attribut 1和value 1)和(attribut 3和value 3)的不同id(s)和名称

在这种情况下,结果将是1 / name1

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

select * from table1
  where id in(select id
                from table2
               where (attribut, value) in ( (1,1), (3,3) )
               group by id
              having count(*)=2
             )