MySql : 如何跳过子查询中的连接
MySql : how to skip joins in subquery
我写了一个关于查找在法国和德国销售的产品的程序。另一方面,我对我的子查询不满意。我有一种感觉,所有的连接都可以被跳过并且完成得更快,但我找不到这个问题的答案。
代码如下:
select productname
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'France')
and p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'Germany')
group by productname
所以我要求的是尽可能地简化它,但是以基本的方式,或者展示跳过连接等的方法。
https://i.stack.imgur.com/UWlzn.png
select productname
from products p
join [Order Details] od on p.productid=od.ProductID
join orders o on od.OrderID=o.OrderID
join customers c on o.CustomerID=c.CustomerID
where c.Country in ('France', 'Germany')
group by productname
having COUNT(DISTINCT c.Country) = 2;
我写了一个关于查找在法国和德国销售的产品的程序。另一方面,我对我的子查询不满意。我有一种感觉,所有的连接都可以被跳过并且完成得更快,但我找不到这个问题的答案。
代码如下:
select productname
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'France')
and p.ProductID in (select p.productid
from products p
join [Order Details] od on p.productid = od.ProductID
join orders o on od.OrderID = o.OrderID
join customers c on o.CustomerID = c.CustomerID
where c.Country LIKE 'Germany')
group by productname
所以我要求的是尽可能地简化它,但是以基本的方式,或者展示跳过连接等的方法。
https://i.stack.imgur.com/UWlzn.png
select productname
from products p
join [Order Details] od on p.productid=od.ProductID
join orders o on od.OrderID=o.OrderID
join customers c on o.CustomerID=c.CustomerID
where c.Country in ('France', 'Germany')
group by productname
having COUNT(DISTINCT c.Country) = 2;