我有一个名为Order的表
OrderID CustomerID IsPaid
--------------------------------------------
1 12 no
2 12 yes
3 13 no
4 13 no
5 14 yes
6 14 yes
我有一个名为Customer
的表 CustomerID Cust_name
--------------------------------
12 John
13 Nick
14 Paul
我需要分散那些已经为订单付款但尚未为之前的订单付款的客户,所以结果应该是
CustomerID Cust_name
--------------------------------
12 John
答案 0 :(得分:1)
您可以进行聚合:
select c.CustomerID, c.Cust_name
from Customer c inner join
Order o
on o.CustomerID = c.CustomerID
group by c.CustomerID, c.Cust_name
having min(o.IsPaid) <> max(o.IsPaid);
答案 1 :(得分:0)
这样,您将仅在存在的情况下获取输出
SELECT *
FROM customer
INNER JOIN order
ON customer.customerid = order.customerid
WHERE ispaid = 'yes';
答案 2 :(得分:0)
您可以尝试以下方法:
SELECT DISTINCT
cust.CustomerID,
cust.Cust_name
FROM customer cust
JOIN order paid
ON cust.CustomerID = paid.CustomerID
AND paid.IsPaid = 'yes'
JOIN order unpd
ON cust.CustomerID = unpd.CustomerID
AND unpd.IsPaid = 'no'
AND unpd.OrderID < paid.OrderID
ORDER BY cust.CustomerID,
cust.Cust_name;
执行以下操作可能会更便宜:
SELECT DISTINCT
cust.CustomerID,
cust.Cust_name
FROM customer cust
JOIN order paid
ON cust.CustomerID = paid.CustomerID
AND paid.IsPaid = 'yes'
WHERE EXISTS (SELECT *
FROM order unpd
WHERE cust.CustomerID = unpd.CustomerID
AND unpd.IsPaid = 'no'
AND unpd.OrderID < paid.OrderID)
ORDER BY cust.CustomerID,
cust.Cust_name;
答案 3 :(得分:0)
由于您的问题明确指出“未为之前的订单付款”,因此窗口功能应该是您的首选!
SELECT *
FROM
(
SELECT *,
LAG(IsPaid) OVER (PARTITION BY CustomerID ORDER BY OrderId) AS IsPreviousOrderPaid
FROM #Orders AS o
) ordersWithPrevious
INNER JOIN #Customer AS c
ON c.CustomerId = ordersWithPrevious.CustomerID
WHERE ordersWithPrevious.IsPaid = 1
AND ordersWithPrevious.IsPreviousOrderPaid = 0;
答案 4 :(得分:0)
您可以做这样的事情。
{
"_id" : "i34908s",
"lifeStageCourses" : [
{
"lifeStage" : "in_school",
"tags" : [
"IIT-182"
],
"courseIds" : []
},
{
"lifeStage" : "in_college",
"tags" : [
"IIT-134",
"IIT-140",
"IIT-395"
],
"courseIds" : []
},
{
"lifeStage" : "prep_entrance_exam",
"tags" : [],
"courseIds" : []
},
{
"lifeStage" : "job_seeker",
"tags" : [
"IIT-134",
"IIT-395"
],
"courseIds" : []
}
]
}
在线演示:Link
SELECT t1.customerid,
Max(cust_name) AS cust_name
FROM table1 t1
INNER JOIN table2 t2 using (customerid)
GROUP BY t1.customerid
HAVING Group_concat(ispaid) = "no,yes"