我只是在mysql中创建一些复杂的(是的,在我的pov中作为mysql中的新玩家)查询,包括子选择添加自定义字段。 Okey,我们只需查看查询:
SELECT DISTINCT `cm_reg__`.`TGL`, `cm_reg__`.`JAM`, `cm_reg__`.`ASR`, `cm_reg__`.`CM`, `c_pst`.`NAMA`, `c_pst`.`SEX`, `c_pst`.`AGAMA`, `c_pst`.`EDU`, `c_pst`.`PKJ`, `c_pst`.`ALAMAT`, `c_pst`.`WILAYAH`, `cm_reg__`.`UMUR`, `cm_reg__`.`ASAL`, `cm_reg__`.`NO`, `cm_reg__`.`DR`, `rm_dgs`.`UT`, `cm_reg__`.`DOUT`, `c_pst`.`C_KELUARGA`, `TINDAKAN`, `PENUNJANG`, `IGD`, `TERAPI`, IFNULL(TINDAKAN, `0)` + IFNULL(PENUNJANG, `0)` + IFNULL(IGD, `0)` + IFNULL(TERAPI, `0)` AS TOTAL
FROM (`cm_reg__`, (SELECT g_sr1301.*, `cm_reg__`.`ASR` AS `ASU`,
SUM(CASE WHEN UT LIKE '1%' OR UT LIKE '2%' OR UT LIKE '5%' OR UT LIKE '6%' OR UT LIKE '8%' OR (UT BETWEEN '02' AND '09') OR (UT BETWEEN '36' AND '37') THEN `CHG` END) AS `TINDAKAN`,
SUM(CASE WHEN UT BETWEEN 31 AND 34 THEN `CHG` END) AS `PENUNJANG`,
SUM(CASE WHEN UT = 01 THEN `CHG` END) AS `IGD`,
SUM(CASE WHEN UT = 30 THEN `CHG` END) AS `TERAPI`
FROM g_sr1301
JOIN cm_reg__ ON g_sr1301.CREG = cm_reg__.`NO`
GROUP BY CREG ) AS tabelbiaya)
JOIN `c_pst` ON `cm_reg__`.`CM` = `c_pst`.`CM`
JOIN `rm_dgs` ON `cm_reg__`.`NO` = `rm_dgs`.`REGISTER`
WHERE `cm_reg__`.`TGL` BETWEEN '2013-01-02' AND '2013-01-02' AND `cm_reg__`.`ASR` = 'ak' AND `cm_reg__`.`DOUT` > cm_reg__.TGL
GROUP BY `NO` ORDER BY `TGL` ASC
我想要的只是一张这样的表:
------- -------- --------------- --------------- --------- ------------ -----------
!CREG ! TGL ! ... !CF1 (TINDAKAN) !CF2(PENUNJANG) !CF3(IGD) !CF4(TERAPI) !CF5(TOTAL) !
!-------!--------! !---------------!---------------!---------!------------!-----------!
! .. ! .. ! ... ! .. ! .. ! .. ! .. ! .. !
!-------!--------! !---------------!---------------!---------!------------!-----------!
请注意,CF是由SUM生成的自定义字段。
不,在我运行查询后会产生一些警告:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'FROM (`cm_reg__`, (SELECT g_sr1301.*, `cm_reg__`.`ASR` AS `ASU`,
SUM(CASE W' at line 2
任何人都可以帮助我吗?因为我是MySQL的新手。谢谢你。 :)
更新
实际上我使这个查询在Codeigniter模型上工作。我只是意识到问题来自突然出现在IFNULL
行中的反引号:
IFNULL(TINDAKAN, `0)` + IFNULL(PENUNJANG, `0)` + IFNULL(IGD, `0)` + IFNULL(TERAPI, `0)`
然后我只是把CI上的活跃记录欺骗到了“甩掉”。那个反叛。 当然,来自@ PM77的解决方案可能适用于......
工作查询与@NoDisplayName一样。我试过了,那就好了。但这个过程太长了......也许它来自加入表。
答案 0 :(得分:1)
如果没有错,这就是你需要的。
删除
中cm_reg__
之前的空心括号
...'FROM (cm_reg__, (SELECT g_sr1301.*, cm_reg__.ASR AS ASU,
并删除tabelbiaya
中... AS tabelbiaya)
后的近距离括号。将逗号更改为cross join
。试试这个
SELECT DISTINCT cm_reg__.TGL,
cm_reg__.JAM,
cm_reg__.ASR,
cm_reg__.CM,
c_pst.NAMA,
c_pst.SEX,
c_pst.AGAMA,
c_pst.EDU,
c_pst.PKJ,
c_pst.ALAMAT,
c_pst.WILAYAH,
cm_reg__.UMUR,
cm_reg__.ASAL,
cm_reg__.NO,
cm_reg__.DR,
rm_dgs.UT,
cm_reg__.DOUT,
c_pst.C_KELUARGA,
TINDAKAN,
PENUNJANG,
IGD,
TERAPI,
Ifnull(TINDAKAN, 0) + Ifnull(PENUNJANG, 0)
+ Ifnull(IGD, 0) + Ifnull(TERAPI, 0) AS TOTAL
FROM cm_reg__
CROSS JOIN (SELECT g_sr1301.*,
cm_reg__.ASR AS ASU,
Sum(CASE
WHEN UT LIKE '1%'
OR UT LIKE '2%'
OR UT LIKE '5%'
OR UT LIKE '6%'
OR UT LIKE '8%'
OR ( UT BETWEEN '02' AND '09' )
OR ( UT BETWEEN '36' AND '37' ) THEN CHG
END) AS TINDAKAN,
Sum(CASE
WHEN UT BETWEEN 31 AND 34 THEN CHG
END) AS PENUNJANG,
Sum(CASE
WHEN UT = 01 THEN CHG
END) AS IGD,
Sum(CASE
WHEN UT = 30 THEN CHG
END) AS TERAPI
FROM g_sr1301
JOIN cm_reg__
ON g_sr1301.CREG = cm_reg__.NO
GROUP BY CREG) AS tabelbiaya
JOIN c_pst
ON cm_reg__.CM = c_pst.CM
JOIN rm_dgs
ON cm_reg__.NO = rm_dgs.REGISTER
WHERE cm_reg__.TGL BETWEEN '2013-01-02' AND '2013-01-02'
AND cm_reg__.ASR = 'ak'
AND cm_reg__.DOUT > cm_reg__.TGL
GROUP BY NO
ORDER BY TGL ASC