根据mysql php中的不同条件选择列

时间:2018-01-17 00:21:14

标签: php mysql conditional-statements

我需要这段代码的帮助。我做错了什么。我想要一个结果,在当前和之前的revperiod专栏中给出了商业,住宅和农业基本列数量的组。如何完成此代码或更好的任何建议。提前谢谢。

SELECT  (CASE (c.classcode)
         WHEN 'C' THEN 'COMMERCIAL'
         WHEN 'R' THEN 'RESIDENTIAL'
         WHEN 'A' THEN 'AGRICULTURE' END) AS CLASSIFICATION,
        ( SELECT  SUM(a.basic)
            FROM  cashreceiptitem_rpt_online a
            JOIN  cashreceipt_rpt b  ON a.rptreceiptid=b.objid
            JOIN  rptledger c  ON a.rptledgerid=c.objid
            WHERE  b.month='11'
              AND  a.revperiod='current'
        ) AS BC, 
        ( SELECT  SUM(a.basic)
            FROM  cashreceiptitem_rpt_online a
            JOIN  cashreceipt_rpt b  ON a.rptreceiptid=b.objid
            JOIN  rptledger c  ON a.rptledgerid=c.objid
            WHERE  b.month='11'
              AND  a.revperiod='previous'
        ) AS BPC
    FROM  cashreceiptitem_rpt_online a
    JOIN  cashreceipt_rpt b  ON a.rptreceiptid=b.objid
    JOIN  rptledger c  ON a.rptledgerid=c.objid
    WHERE  b.month='11'

我有这个错误Error Code: 1242; Subquery returns more than 1 row.

1 个答案:

答案 0 :(得分:0)

不确定您的表格结构,但您应该可以执行以下操作:

SELECT c.classcode, SUM(a.basic), SUM(aprev.basic)
FROM cashreceiptitem_rpt_online a
JOIN cashreceiptitem_rpt_online aprev 
    ON a.rptreceiptid = aprev.rptreceiptid
JOIN cashreceipt_rpt b ON a.rptreceiptid=b.objid
JOIN cashreceipt_rpt bprev ON a.rptreceiptid=bprev.objid
JOIN rptledger c ON a.rptledgerid=c.objid
WHERE b.month='11' AND bprev.month='11'
AND a.revperiod='current' AND aprev.revperiod = 'previous'
GROUP BY c.classcode;

您需要为表格的主键找出合适的连接。