订单表
+---------+------------+----------------+----------+------------+
| OrderID | CustomerID | DateOfPurchase | Discount | DueDate |
+---------+------------+----------------+----------+------------+
| 82 | 7 | 2022-04-17 | 0 | 2022-05-17 |
| 83 | 91 | 2022-04-17 | 0 | 2022-05-17 |
| 84 | 8 | 2022-04-17 | 0 | 2022-05-17 |
| 85 | 91 | 2022-04-17 | 0 | 2022-05-17 |
| 86 | 7 | 2022-04-17 | 0 | 2022-05-17 |
| 87 | 91 | 2022-04-18 | 0 | 2022-05-18 |
| 109 | 7 | 2022-04-25 | 0 | 2022-05-25 |
+---------+------------+----------------+----------+------------+
Customer表
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
| CustomerID | Fname | Mname | Lname | Contact_no | Address | Valid_id | Credit_Limit |
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
| 7 | John | Dale | Doe | 09123654789 | | NULL | 5000.000 |
| 8 | Jane | Dale | Doe | 09987654123 | | NULL | 1500.000 |
| 91 | Kurdapya | Buang | Selos | 09741258963 | | NULL | 5000.000 |
+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
支付表
+-----------+------------+---------+------------+----------+
| PaymentID | CustomerID | OrderID | PayDate | Amount |
+-----------+------------+---------+------------+----------+
| 20 | 7 | 82 | 2022-04-25 | 800.000 |
| 21 | 91 | 83 | 2022-04-17 | 2500.000 |
| 22 | 91 | 85 | 2022-04-17 | 200.000 |
| 23 | 95 | 88 | 2022-04-18 | 2122.000 |
| 24 | 96 | 90 | 2022-04-25 | 577.000 |
| 25 | 97 | 111 | 2022-04-25 | 0.000 |
| 26 | 98 | 114 | 2022-04-25 | 166.000 |
| 27 | 99 | 115 | 2022-04-25 | 1740.000 |
+-----------+------------+---------+------------+----------+
我想知道客户Kurdapya(OrderID=91)的OrderID是已支付和未支付的
这是我到目前为止尝试过的查询
尝试1:
select if(py.OrderID=r.OrderID, 'paid','unpaid') as remarks, r.OrderID, r.CustomerID
from orders r,
payment py
where py.OrderID = r.OrderID and r.CustomerID = 91
GROUP by r.OrderID;
尝试1的结果:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| paid | 83 | 91 |
| paid | 85 | 91 |
+---------+---------+------------+
尝试2:
select if(py.OrderID=r.OrderID and py.OrderID=py.Amount!='null', 'paid','unpaid') as remarks, r.OrderID, r.CustomerID
from orders r,
payment py
where r.CustomerID = 91
GROUP by r.OrderID;
尝试2的结果:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| unpaid | 83 | 91 |
| unpaid | 85 | 91 |
| unpaid | 87 | 91 |
+---------+---------+------------+
我希望得到的结果是:
+---------+---------+------------+
| remarks | OrderID | CustomerID |
+---------+---------+------------+
| unpaid | 83 | 91 |
| unpaid | 85 | 91 |
| paid | 87 | 91 |
+---------+---------+------------+
3条答案
按热度按时间r6l8ljro1#
You can try
62lalag42#
Your second query didn't even compile. Try this
Points to note:
3bygqnnd3#
However, since the order table doesn't have an amount, I don't know if the amount paid is sufficient to pay for the full order.