简体   繁体   English

如何从多对多表中选择一个ID,而另一个ID中不存在的记录呢?

[英]How to select a record from a many to many table where one id exists in one but not another?

Sorry if that title didn't explain it well. 抱歉,该标题解释得不好。

Here is the table... 这是桌子...

产品ID到类别ID
(source: alexanderdickson.com ) (来源: alexanderdickson.com

I want to select product_id where it is in either 5 or 6 but not if it is in 7 . 我想选择product_id56但如果在7 则不选择

Maybe I've lost the plot, but I came up with this query 也许我丢失了剧情,但是我想到了这个查询

SELECT `product_id` , `category_id`
FROM `wp_wpsc_item_category_assoc`
WHERE (
`category_id` =5
OR `category_id` =6
)
AND `category_id` !=7
LIMIT 0 , 30

Except obviously because of the many to many relationship this will still return a record where category_id is 7, in this case the product with a product_id of 12. 除非显然由于多对多关系,否则仍将返回category_id为7的记录,在这种情况下, product_id为12的产品。

How can I change this query to get all products with either a category_id of 5 or 6 , but not if it is also a part of 7 . 我如何更改此查询以获取category_id56所有产品,但如果它也是7的一部分则不能。

Thanks guys. 多谢你们。

Update 更新

Thanks for all your answers. 感谢您的所有答复。 My daily vote limit is reached, so I'll come back tomorrow and vote up the useful answers. 已达到我的每日投票上限,因此明天我会回来投票支持有用的答案。

To cut off the records with category_id = 7 yous should to check all the records for each product_id. 要切断category_id = 7的记录,您应该检查每个product_id的所有记录。 So you need a subquery to use: 因此,您需要一个子查询才能使用:

SELECT 
    product_id, 
    category_id
FROM wp_wpsc_item_category_assoc AS a
WHERE 
(  category_id = 5
OR category_id = 6)
AND
NOT EXISTS (
        SELECT * 
            FROM wp_wpsc_item_category_assoc AS b 
        WHERE a.product_id = b.product_id 
            AND b.category_id = 7
    )
SELECT product_id, category_id
FROM   wp_wpsc_item_category_assoc WINC
WHERE  WINC.category_id IN (5, 6)
AND    NOT EXISTS
    (SELECT 0 FROM wp_wpsc_item_category_assoc WEXC
     WHERE WEXC.product_id = WINC.product_id
     AND WEXC.category_id IN (7))

We use a subquery to do the exclusion for each particular product_id. 我们使用子查询对每个特定的product_id进行排除。

I'm not sure I fully understand the question but maybe you need to use a union or a sub query. 我不确定我是否完全理解这个问题,但也许您需要使用并集或子查询。

The first think I suggest would be something like: 我建议的第一个想法是:

SELECT product_id , category_id
FROM
 (SELECT product_id , category_id
 FROM wp_wpsc_item_category_assoc
 WHERE product_id !=7)
WHERE (
product_id =5
OR product_id =6
)

I can't test this right now so the brackets might be out slightly but it might do what you're looking for. 我现在无法对此进行测试,因此括号可能会稍微漏出一点,但它可能会满足您的需求。

Your question says you're interested in the product id, but the sample you gave examined the category, switch if necessary... 您的问题表明您对产品ID感兴趣,但是您提供的样品检查了类别,如有必要,请切换...

This works on SQL Server and (should be) portable to MySQL. 这适用于SQL Server,并且(应该)可移植到MySQL。

The gist of the solution is to exclude all products that are in any way linked to category 7. 解决方案的要点是排除以任何方式链接到类别7的所有产品。

SELECT  Product_ID
        , Category_ID
FROM    wp_wpsc_item_category_assoc a
        LEFT OUTER JOIN (
          SELECT  Product_ID
          FROM    wp_wpsc_item_category_assoc
          WHERE   Category_ID = 7
        ) anot ON anot.Product_ID = a.ProductID
WHERE   anot.Product_ID IS NULL
        AND a.Category_ID IN (5, 6)        

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从多对多表中选择一对一关系 - How to select one to one relations from many to many table 与确保至少存在1条记录的一对多关系(许多表) - One to many relationship with ensuring at least 1 record exists in (many table) 从一个表中选择在另一个表中存在映射的行 - Select rows from one table where mapping exists in another table 如何从一个主表中获取一条记录,以及如何以一对多的关系从另一张表中获取有关该表的详细信息? - How to get a single record from main table and details about it from another table in a one:many relation? 在 SQL、Select 中,来自一对多表的所有 FK,其中 FK 的表数据存在于每个列表/连接中 - In SQL, Select all FKs from one-to-many table where table data for FK Exists in each of several lists/joins 一对多关系中不存在的地方 - Where Not Exists With One to Many Relationship 如何从一对多关系的“多”表中选择具有两个特定子ID的父ID? - How can I select parent id's that have two specific child id's from the “many” table of a one-to-many relationship? 从用户ID不为IN的表(另一个表)中选择*作为ALLIDS,同时还要计算它们在另一个表中出现的次数 - Select * as ALLIDS from table where user ID NOT IN (another table) while also counting how many times they occur in another table 从许多具有相同ID的行中选择一行 - select one row from many with same id 如何从与另一个表具有一对多关系的表中选择特定列? - How to select specific column from a table that has one to many relationship with another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM