gpt4 book ai didi

MySQL 选择列(如果存在)

转载 作者:行者123 更新时间:2023-11-30 00:53:58 25 4
gpt4 key购买 nike

我已经进行了以下查询,但是当我尝试再选择一列时,我没有得到任何结果。我只是不知道如何选择该列(if 语句就是有问题的列)。

问题是 cash_ payment_detail 表中的 sub_acc 列也可以为 null。任何帮助都意义重大。请注意,我仍在学习中。

SELECT
LPAD(cash_payments_detail.`control_acc`,5,0) AS Control_Account,
CAccountDescription As Control_Account_Name,
LPAD(cash_payments_detail.`sub_acc`,4,0) AS Sub_Account,
**if (coasub.`SAccountNo` = cash_payments_detail.`sub_acc`, coasub.`SAccountDescription`," ") As Sub_Account_Name**
FROM
coasub, coacontrol, `cash_payments_detail` cash_payments_detail INNER JOIN `cash_payments` cash_payments ON cash_payments_detail.`cpvno` = cash_payments.`cpvno`
WHERE
control_acc = coacontrol.`CAccountNo`

最佳答案

您的问题在于您是否必须在编写此类查询之前很好地学习连接,并注意您编写的连接在以下情况下不会返回记录:cash_ payment_detail.sub_acc 为空左连接将返回空值;)使用左连接而不是内连接...尝试这个查询:

SELECT
cash_payments.cpvno AS Voucher_Number, date_format(cash_payments.vdate,'%d %M %Y') AS Voucher_Date,
sec_users.name AS Entered_By, cash_payments.payto AS Payment_To, cash_payments.payref AS On_Account_Of, LPAD(cash_payments.branch,5,0) AS Cash_Control_Account, branches.cash_control_name AS Cash_Control_Name,
branches.name AS Branch_Name,
LPAD(cash_payments_detail.control_acc,5,0) AS Control_Account, CAccountDescription As Control_Account_Name,
LPAD(cash_payments_detail.sub_acc,4,0) AS Sub_Account, if (SAccountNo = sub_acc, SAccountDescription," ") As Sub_Account_Name,
cash_payments_detail.job_no AS Job, cash_payments_detail.narration AS Narration, cash_payments_detail.debit AS Debit
FROM
coasub, coacontrol, branches, sec_users, cash_payments_detail cash_payments_detail
LEFT JOIN cash_payments cash_payments ON cash_payments_detail.cpvno = cash_payments.cpvno
WHERE
control_acc = coacontrol.CAccountNo AND cashcontrol = cash_payments.branch AND cash_payments.login = sec_users.login

关于MySQL 选择列(如果存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20739456/

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