从一个表中选择行,其中另一个表中的多个行已确定值

时间:2013-12-24 12:17:08

标签: mysql sql

我在MySQL中有两个表:
产品:

  id  |  value
================
   1  |   foo
   2  |   bar   
   3  |   foobar
   4  |   barbar

和属性:

  product_id  |  property_id
=============================
       1      |       10
       1      |       11
       2      |       15     
       2      |       16
       3      |       10    
       3      |       11
       4      |       10
       4      |       16

我想获得具有确定属性的产品。 例如,我需要获得所有具有ids 10和11属性的产品。我希望产品的ID为1和3而不是4!

在mysql中是否可能或我需要使用PHP?

谢谢!

5 个答案:

答案 0 :(得分:2)

  

使用ids 10和11

这是两个解决方案:

SELECT p.id, 
       p.value, 
       Count(DISTINCT propety_id) 
FROM   products p 
       INNER JOIN properties pr 
               ON p.id = pr.product_id 
                  AND propety_id IN ( 10, 11 ) 
HAVING Count(DISTINCT propety_id) = 2; 

...或

SELECT p.id, 
       p.value 
FROM   products p 
       INNER JOIN properties pr1 
               ON p.id = pr2.product_id 
                  AND pr1.propety_id = 10 
       INNER JOIN properties pr2 
               ON p.id = pr2.product_id 
                  AND pr2.propety_id = 11; 

至于排除行 - 添加NOT exists子句,或者执行额外的左连接并排除匹配的行。

答案 1 :(得分:2)

SELECT * 
FROM   [products] 
WHERE  id IN (SELECT product_id 
              FROM   [properties] 
              WHERE  propety_id IN ( '10', '11' ) 
              HAVING Count(DISTINCT propety_id) = 2); 

答案 2 :(得分:1)

试试这个:

SELECT p.id 
FROM   product p 
       INNER JOIN properties prop 
               ON p.id = prop.product_id 
                  AND property_id IN ( 10, 11 ) 
GROUP  BY p.id 
HAVING Count(DISTINCT property_id) = 2 

答案 3 :(得分:1)

以下是我解决它的方法:

mysql> SELECT * FROM products;
+----+--------+
| id | value  |
+----+--------+
|  1 | foo    |
|  2 | bar    |
|  3 | foobar |
|  4 | barbar |
+----+--------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM properties;
+------------+-------------+
| product_id | property_id |
+------------+-------------+
|          1 |          10 |
|          1 |          11 |
|          2 |          15 |
|          2 |          16 |
|          3 |          10 |
|          3 |          11 |
|          4 |          10 |
|          4 |          16 |
+------------+-------------+
8 rows in set (0.00 sec)

现在我们选择所有产品ID,它们具有property_ids IN(10,11)并且具有2个不同的property_id行:

mysql> SELECT
        product_id FROM properties 
WHERE
        properties.property_id IN (10, 11) 
GROUP BY
      product_id 
HAVING
      COUNT(DISTINCT property_id) = 2;

+------------+
| product_id |
+------------+
|          1 |
|          3 |
+------------+
2 rows in set (0.01 sec)

将此查询与产品中的SELECT-ing结合使用:

mysql> SELECT
        id, value
FROM
        products
WHERE
        products.id IN(
SELECT
        product_id FROM properties 
WHERE
        properties.property_id IN (10, 11) 
GROUP BY
      product_id 
HAVING
      COUNT(DISTINCT property_id) = 2);

+----+--------+
| id | value  |
+----+--------+
|  1 | foo    |
|  3 | foobar |
+----+--------+
2 rows in set (0.00 sec)

sql fiddle

答案 4 :(得分:0)

试试这个

SELECT Prd.*
FROM products As Prd
LEFT JOIN (SELECT product_id ,SUM(RStatus) As Tt
            FROM (SELECT product_id,
                CASE 
                    WHEN propety_id = 10 THEN NULL
                    WHEN propety_id = 11 THEN NULL
                    ELSE 0
                END  As RStatus
            FROM properties 
                  ) A
        GROUP BY product_id
        ) AS Prt ON(Prd.ID = Prt.product_id )
WHERE Prt.Tt IS  NULL