从同一属性sql中联合和联接3种类型

rwqw0loc  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(319)

我有下列表格
客户(电子邮件、姓名、地址、生日)产品(产品ID、说明、价格、重量、类型)订单(订单ID、客户、日期、付款)订单内容(订单ID、产品、金额)
问题是
找到电子邮件和客户的名字谁已经作出了至少两个订单不同的付款方式。每个客户只列出一次。有3种支付方式(mobilepay、credit、paypal)
有人能帮我吗?
我用过这个密码

SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("mobilepay")

UNION

SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("credit")

INTERSECT
SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("paypal")

UNION

SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("credit")

UNION 
SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("paypal")

INTERSECT 
SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment=("mobilepay")
ubof19bj

ubof19bj1#

选择客户名称和电子邮件,其中订单数据集至少有两种不同的付款类型。

select customer.name, customer.email
from customer customer
cross apply
(
  select count(distinct(orders2.Payment)) PaymentType from orders orders2
  where customer.customerID=orders1.customerID
  group by orders2.Payment
  having(count(distinct(orders2.Payment)))>1
)orders
dldeef67

dldeef672#

你应该通过having子句来研究这两个组,having子句允许你过滤汇总的数据。
如果您只有3种付款方式,那么根本不需要where条款。

ru9i0ody

ru9i0ody3#

我想你必须使用分组和计数有三种不同付款方式的客户数
顺便说一下,您可以在 WHERE 条款

SELECT Customer.email, Customer.name
FROM Customer
JOIN Orders
ON Orders.customer=Customer.email
WHERE Orders.payment in ("mobilepay", "credit", "paypal")

请注意,我没有给你答案,但只有一些提示

相关问题