Mysql 多对多表的问题
我有3张表,如下所示:Mysql 多对多表的问题,mysql,sql,Mysql,Sql,我有3张表,如下所示: [Products table] 1 -> many [Ratings table] many < - 1 [Customers table] 但这并没有淘汰其他客户评级相同的产品。现在如果我有一台HP 550笔记本电脑,不同的客户对它进行了两次评级, 此查询将只踢出@CustomerID评级的评级,而不会踢出第二个评级。有人能帮我吗?所有未经@customer评级的产品: select p.ID, p.Name from products
[Products table] 1 -> many [Ratings table] many < - 1 [Customers table]
但这并没有淘汰其他客户评级相同的产品。现在如果我有一台HP 550笔记本电脑,不同的客户对它进行了两次评级,
此查询将只踢出@CustomerID评级的评级,而不会踢出第二个评级。有人能帮我吗?所有未经@customer评级的产品:
select
p.ID,
p.Name
from
products p
where
p.ID not in
(select distinct ratings.productID from ratings
where ratings.customerID = @CustomerID)
select
p.ID,
p.Name,
r.*
from
products p
inner join
ratings r on p.productID = r.ProductID
where
p.ID not in
(select distinct ratings.productID from ratings
where ratings.customerID = @CustomerID)
非@customer评级产品的所有评级:
select
p.ID,
p.Name
from
products p
where
p.ID not in
(select distinct ratings.productID from ratings
where ratings.customerID = @CustomerID)
select
p.ID,
p.Name,
r.*
from
products p
inner join
ratings r on p.productID = r.ProductID
where
p.ID not in
(select distinct ratings.productID from ratings
where ratings.customerID = @CustomerID)
比如:
select p.*
from products p
where not exists (
select 1
from ratings r
where r.customerid = ?
and r.productid = p.productid
)
如果您想用连接来表示这一点:
select p.*
from products p
left join ratings r
on r.customerid = ?
and r.productid = p.productid
where r.productid is null
像往常一样,考虑提供适当的DDL和/或SqLFIDLE连同期望的结果集。我不明白,你是什么意思?