[英]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... 这是桌子...
(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_id
在5
或6
但如果在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_id
为5
或6
所有产品,但如果它也是7
的一部分则不能。
Thanks guys. 多谢你们。
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.