Sql 相同列上的内部联接
预期结果Sql 相同列上的内部联接,sql,oracle,Sql,Oracle,预期结果 Customer Table ---------------------- CustomerName Peter Sam Sales Table ----------------------- ProductName Customer Cloth Peter Mobile Peter Cloth Sam Laptop Sam 我想作为购买“衣服”而不是“手机”的客户,我试过了 Customer Sam
Customer Table
----------------------
CustomerName
Peter
Sam
Sales Table
-----------------------
ProductName Customer
Cloth Peter
Mobile Peter
Cloth Sam
Laptop Sam
我想作为购买“衣服”而不是“手机”的客户,我试过了
Customer
Sam
但它总是同时返回两个条目
select c.CustomerName from Customer c inner join Sales s1 on (s1.customer = c.customername and s1.productname = 'Cloth') inner join Sales s2 on (s2.customer = c.customername and s2.productname != 'Mobile');
试试这个:
Customer
Peter
Sam
Sam
试试这个:
Customer
Peter
Sam
Sam
关联子查询会更好,因为您不想为多次购买衣服的客户获取多行
select c.CustomerName
from Customer c
where exists(select 1 from sales s1 where s1.customer = c.customername and s1.productname = 'Cloth')
and not exists (select 1 from sales s2 where s2.customer = c.customername and s2.productname = 'Mobile')
关联子查询会更好,因为您不想为多次购买衣服的客户获取多行
select c.CustomerName
from Customer c
where exists(select 1 from sales s1 where s1.customer = c.customername and s1.productname = 'Cloth')
and not exists (select 1 from sales s2 where s2.customer = c.customername and s2.productname = 'Mobile')
您可以使用Oracle操作符使其变得简单
select
c.CustomerName
from
Customer c
where
exists (
select null
from sales
where sales.customer = c.customername and
s1.productname = 'Cloth') and
not exists (
select null
from sales
where sales.customer = c.customername and
s1.productname = 'Mobile');
另一个稍微复杂一点的选项是左连接
SELECT "Customer" FROM SalesTable WHERE "ProductName"='Cloth'
MINUS
SELECT "Customer" FROM SalesTable WHERE "ProductName"='Mobile'
.您可以使用Oracle操作符使其变得简单
select
c.CustomerName
from
Customer c
where
exists (
select null
from sales
where sales.customer = c.customername and
s1.productname = 'Cloth') and
not exists (
select null
from sales
where sales.customer = c.customername and
s1.productname = 'Mobile');
另一个稍微复杂一点的选项是左连接
SELECT "Customer" FROM SalesTable WHERE "ProductName"='Cloth'
MINUS
SELECT "Customer" FROM SalesTable WHERE "ProductName"='Mobile'
.首先,您应该查看您的数据库架构。
你永远不会在没有id的情况下进行内部连接。 尝试使用关系创建表。像这样:
SELECT DISTINCT s1."Customer"
FROM SalesTable s1
LEFT JOIN SalesTable s2
ON s1."Customer" = s2."Customer"
AND s2."ProductName" = 'Mobile'
WHERE s1."ProductName" = 'Cloth'
AND s2."Customer" IS NULL;
嗯,
如果无法执行此操作,则可以(以旧方式)执行以下查询:
我希望能帮助你。
拥抱,
Vin。首先,您应该查看数据库架构。
你永远不会在没有id的情况下进行内部连接。 尝试使用关系创建表。像这样:
SELECT DISTINCT s1."Customer"
FROM SalesTable s1
LEFT JOIN SalesTable s2
ON s1."Customer" = s2."Customer"
AND s2."ProductName" = 'Mobile'
WHERE s1."ProductName" = 'Cloth'
AND s2."Customer" IS NULL;
嗯,
如果无法执行此操作,则可以(以旧方式)执行以下查询:
我希望能帮助你。
拥抱,
Vin。这是“集合内集合”查询的一个示例。我认为一个好方法是使用聚合:
select Customer.customername
from Customer
inner join on (customer.customername = sales.customer)
where sales.productname = 'Cloth'
我更喜欢将逻辑放在having子句中,因为它非常灵活。您可以很容易地为其他产品添加附加条件。这是“集合内集合”查询的一个示例。我认为一个好方法是使用聚合:
select Customer.customername
from Customer
inner join on (customer.customername = sales.customer)
where sales.productname = 'Cloth'
我更喜欢将逻辑放在having子句中,因为它非常灵活。您可以很容易地为其他产品添加附加条件。谢谢@David使用InnerJoin不可能吗?您必须将outer join添加到sales以查找“Mobile”的销售额,然后从结果中删除找到的任何记录,并将InnerJoin添加到“Cloth”,然后区分结果集。这一点效率都没有。谢谢@David,使用innerjoins这是不可能的吗?您必须将外部连接到sales以查找“Mobile”的销售额,然后从结果中删除找到的任何记录,将内部连接到“Cloth”,然后区分结果集。这根本就没有效率。