gpt4 book ai didi

mysql - 如何使用 IF() 条件在 MySQL 中显示真行和假行

转载 作者:行者123 更新时间:2023-12-05 02:29:50 24 4
gpt4 key购买 nike

订单表

+---------+------------+----------------+----------+------------+
| 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 |
+---------+------------+----------------+----------+------------+

客户表

+------------+----------+-------+-------+-------------+-----------------------------+----------+--------------+
| 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 |
+---------+---------+------------+

最佳答案

SELECT
IF(py.OrderID IS NULL, 'unpaid', 'paid') AS remarks,
r.OrderID,
r.CustomerID
FROM orders AS r
LEFT OUTER JOIN payment AS py USING (OrderID)
WHERE r.CustomerID = 91

但是,由于订单表没有金额,我不知道支付的金额是否足以支付全部订单。

关于mysql - 如何使用 IF() 条件在 MySQL 中显示真行和假行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72015347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com