获取没有连接的记录

时间:2012-05-18 20:54:55

标签: sql

我有3张桌子:ProductsProductPropertiesProperties。我需要获得所有没有“Property B”的产品。表:

# Products          # Join table                  # Properties
+----+-----------+  +------------+-------------+  +-------------+---------------+
| id | name      |  | product_id | property_id |  | property_id | property_name |
+----+-----------+  +------------+-------------+  +-------------+---------------+
|  1 | Product 1 |  |          1 |           1 |  |           1 |   Propeprty A |
|  2 | Product 2 |  |          1 |           2 |  |           2 |   Propeprty B |
|  3 | Product 3 |  |          2 |           1 |  +-------------+---------------+
+----+-----------+  |          2 |           2 |
                    |          3 |           1 |
                    +------------+-------------+

在这种特殊情况下我希望返回产品3

是否可以在单个数据库查询中获取所有必需的产品?什么是最小的查询可以实现的目标?

被修改即可。子查询的查询被认为是

5 个答案:

答案 0 :(得分:1)

select * from Products P where not exists (select * from ProductProperties 
inner join Properties on ProductProperties .property_id = Properties property_id 
where P.product_id = ProductProperties.product_id and property_name = 'Propeprty B') -- *or whatever* 

答案 1 :(得分:1)

  

我需要获得所有没有“Property B”的产品

反转问题。找到 PropertyB,然后否定它。

从所有PropertyB属性开始:

SELECT
     Property_Id
FROM Properties 
WHERE
     Property_Name = 'Property B'

然后,找到ProductIdProperty_Id

SELECT
     ProductId
FROM ProductProperties
JOIN (
     --1st query
     SELECT
         Property_Id
     FROM Properties 
     WHERE
         Property_Name = 'Property B'
) as PropertyB ON
    ProductProperties.Property_Id = PropertyB.Property_Id  

然后,查找不在该集合中的所有Product

SELECT
    ProductId
FROM Product
LEFT OUTER JOIN (
    --2nd query
    SELECT
        ProductId
    FROM ProductProperties
    JOIN (
        --1st query
        SELECT
            Property_Id
        FROM Properties 
        WHERE
            Property_Name = 'Property B'
    ) as PropertyB ON
       ProductProperties.Property_Id = PropertyB.Property_Id
) as ProductsWithPropertyB ON
     Products.ProductId = ProductsWithPropertyB.ProductId
WHERE
     ProductsWithPropertyB.ProductId IS NULL

然后,你可以简化一下:

SELECT
    ProductId
FROM Product
LEFT OUTER JOIN (
    SELECT
        ProductId
    FROM ProductProperties
    JOIN Properties ON
        ProductProperties.PropertyId = Properties.PropertyId
    WHERE
        Properties.Name = 'Property B'
) as ProductsWithPropertyB ON
     Products.ProductId = ProductsWithPropertyB.ProductId
WHERE
     ProductsWithPropertyB.ProductId IS NULL

或者,如果您更喜欢IN子句(服务器可能不关心):

SELECT 
     ProductId 
FROM Products
WHERE
     ProductId NOT IN (
          SELECT ProductId FROM ProductProperties WHERE PropertyId IN (
              SELECT PropertyId FROM Properties WHERE PropertyName = 'Property B'
          )
     )

答案 2 :(得分:0)

SELECT Products.ProductID, Products.Name, Properties.property_name FROM Products
INNER JOIN ProductProperties ON Products.product_ID = ProductProperties.product_ID
INNER JOIN Properties ON ProductProperties.Property_ID = Properties.Property_ID
WHERE Properties.Property_ID <> 2

答案 3 :(得分:0)

好像你想要一个OUTER JOIN然后一个WHERE子句来恢复不匹配的属性。这是未经测试的,但是:

SELECT Products.* FROM 
Products RIGHT OUTER JOIN Join_Table 
  ON Products.ID = Join_Table.Product_ID
  AND Join_Table.Property_ID = 2
WHERE Products.ID IS NULL

答案 4 :(得分:-1)

是(已更正以删除潜艇),

    Select * from 
     Products pr
     left join jointable jt on pr.id = jt.product_id 
     join properties pr on jt.property_id = pr.property_id and pr.property_name = 'Property B'
 where jt.product_id is null