繁体   English   中英

如何在比较sql中同一个表中的两列时进行选择

[英]How to select while comparing two columns from the same table in sql

CREATE VIEW mainT 
AS SELECT transactionId AS accountId, glbalance AS debit 
IF(totalDebit > totalCredit), glbalance as Credit IF(totalCredit > totalDebit) 
FROM `trialbalanceView`

是否可以在SQL中生成这种查询? 请任何帮助,我们将不胜感激。

你想要一个CASE声明似乎......

CREATE VIEW mainT 
AS 
SELECT 
    transactionId AS accountId, 
    glbalance AS debit,
    case when totalDebit > totalCredit then glbalance else null end as Debit,
    case when totalCredit > totalDebit then glbalance else null end as Credit
FROM trialbalanceView

我相信你所寻找的东西类似于下面的东西。 我假设您的问题与基于逻辑操作的条件列值有关。 如果该假设不正确,请提供更多详细信息,说明需要通过示例和基表数据解决的问题究竟是什么。

CREATE VIEW mainT 
AS 
SELECT transactionId AS accountId, 
    CASE WHEN totalDebit > totalCredit
        THEN glbalance 
    ELSE null AS debit, 
    CASE WHEN totalDebit < totalCredit
        THEN glbalance 
    ELSE null AS credit
FROM trialbalanceView

谢谢,K

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM