Sewaktu pemanggilan data tersebut saya tidak ingin terlalu banyak script perulangan di coding yang saya buat, akhirnya saya membuat single query MySQL. Scriptnya seperti ini :
$sql = " SELECT sum(nominal) FROM
(
SELECT replid, idbesarjtt, idjurnal,
SUBSTRING_INDEX(SUBSTRING_INDEX(diskon, ',', n.n), ',', -1) diskon,
tanggal, jumlah, keterangan, petugas, alasan, info1,
SUBSTRING_INDEX(SUBSTRING_INDEX(info2, ',', n.n), ',', -1) nominal,
info3, ts, token, issync
FROM penerimaanjtt CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE n.n <= 1 + (LENGTH(diskon) - LENGTH(REPLACE(diskon, ',', '')))
)
penerimaanjtt, besarjtt, masterdiskonpenerimaan, jurnal
WHERE penerimaanjtt.idbesarjtt = besarjtt.replid
AND penerimaanjtt.idjurnal = jurnal.replid
AND penerimaanjtt.diskon = masterdiskonpenerimaan.replid
AND jurnal.idtahunbuku = '$idtahunbuku'
AND FIND_IN_SET($idp,penerimaanjtt.diskon) <> 0
AND masterdiskonpenerimaan.departemen='$dept'
AND penerimaanjtt.tanggal BETWEEN '$tanggal1' AND '$tanggal2'
$sql_idpetugas";
Semoga bermanfaat.
Post a Comment