在 MySQL 中使用过滤器内部连接多对多表 [英] Inner Join Many to Many Tables with Filter In MySQL
问题描述
表结构....这里只有 3 个表..(购买的、客户的、产品的)...
table structure ....Here is only 3 tables.. (bought ,customer,product). ..
bought table structure : fields (id,product_id,customer_id),
customer table structure: (customer_id,name,address) ,
product table structure: (product_id,name).
产品表内包含产品 a 和 b.从购买的表中,我想获取客户 ID(购买产品a"但未购买产品b"的客户).
inside the product table included product a and b. From the bought table i want to get customer id (customers who bought product 'a' but did not buy products 'b').
使用 mysql 或类似工具,如何在不使用子选择的情况下选择购买产品a"但未购买产品b"的所有客户.(确保结果中没有客户购买了产品b".)..请帮助我
Using mysql or similar, how would you select all customers who bought product 'a' but did not buy products 'b', Without using subselects. (make sure there are no customers bought product 'b' in the result.) ..please help me
推荐答案
我不确定,您想按产品名称过滤吗?也许我在你的问题中遗漏了一些东西,但我认为这应该有效
Im not sure, you want to filter by product name? Maybe i'm missing something in your question, but i think this should work
select customer_id
from
bought b
inner join customer c on b.customer_id = b.customer_id
inner join product p on p.product_id = b.product_id
where p.name = 'a'
这篇关于在 MySQL 中使用过滤器内部连接多对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!