combining two tables query

Steven C. :

i came across the following problem

i would like to sum someone absence using mysql with across within these twop codes

SELECT  a.nama_account,
             a.jabatan,
             b.nama_tp,
             a.jam_berangkat,
             a.jam_pulang,
             (SUM(IF((jam_awal < jam_berangkat) AND (jam_akhir > jam_pulang),'1','0'))+dinas+dinastp) jum,
             DATEDIFF('$tgl2','$tgl1')+1 jumhar
            FROM t_account a
            LEFT JOIN t_tp b ON a.tp_ID=b.ID
            LEFT JOIN t_user c ON a.userCode=c.userCode
            LEFT JOIN (
                        SELECT  DATE(`tanggal`) tgl,
                            phoneNumber,
                            MIN(TIME( tanggal)) jam_awal,
                            MAX(TIME( tanggal)) jam_akhir,
                            SUM(IF(saran = '1234','1','0')) dinas,
                            SUM(IF(saran = '4321','1','0')) dinastp
                        FROM    t_report 
                        WHERE ( tanggal BETWEEN '$tgl1 00:00:00' AND '$tgl2 23:59:59' )
                        GROUP BY DATE(tanggal),phoneNumber
                     ) tlis
                 ON tlis.phoneNumber = a.`imei_number`
            WHERE a.grup_ID = '$grup'
            $queryIf
            AND (c.user_status !='3' AND c.user_status != '999') 
            GROUP BY a.`imei_number`
            ORDER BY b.nama_tp,a.jabatan

AND

SELECT  a.nama_account,
             a.jabatan,
             b.nama_tp,
             a.jam_berangkat,
             a.jam_pulang,
             (SUM(IF((jam_awal < jam_berangkat) AND (jam_akhir > '$jampulang_sabtu'),'1','0'))+dinas+dinastp) jum,
             DATEDIFF('$tgl2','$tgl1')+1 jumhar
            FROM t_account a
            LEFT JOIN t_tp b ON a.tp_ID=b.ID
            LEFT JOIN t_user c ON a.userCode=c.userCode
            LEFT JOIN (
                        SELECT  DATE(`tanggal`) tgl,
                            phoneNumber,
                            MIN(TIME( tanggal)) jam_awal,
                            MAX(TIME( tanggal)) jam_akhir,
                            SUM(IF(saran = '1234','1','0')) dinas,
                            SUM(IF(saran = '4321','1','0')) dinastp
                        FROM    t_report 
                        WHERE ( tanggal BETWEEN '$tgl1 00:00:00' AND '$tgl2 23:59:59') AND DAYNAME(tanggal)='Saturday'
                        GROUP BY DATE(tanggal),phoneNumber
                     ) tlis
                 ON tlis.phoneNumber = a.`imei_number`
            WHERE a.grup_ID = '$grup'
            $queryIf
            AND (c.user_status !='3' AND c.user_status != '999') 
            GROUP BY a.`imei_number`
            ORDER BY b.nama_tp,a.jabatan

i wanna get the combine number of jum in each query, is there anything i can do with 1 query instead having these two? because its a trouble to loopin it on php echo.

thanks

Gharbad The Weak :

@StevenC. it sounds like you need to use a UNION to put the results of the 2 queries together.

Here's how you would do it:

SELECT nama_account,
    jabatan,
    nama_tp,
    jam_berangkat,
    jam_pulang,
    jumhar,
    SUM(jum) AS jumTotal
FROM (
SELECT  a.nama_account,
             a.jabatan,
             b.nama_tp,
             a.jam_berangkat,
             a.jam_pulang,
             (SUM(IF((jam_awal < jam_berangkat) AND (jam_akhir > jam_pulang),'1','0'))+dinas+dinastp) jum,
             DATEDIFF('$tgl2','$tgl1')+1 jumhar
            FROM t_account a
            LEFT JOIN t_tp b ON a.tp_ID=b.ID
            LEFT JOIN t_user c ON a.userCode=c.userCode
            LEFT JOIN (
                        SELECT  DATE(`tanggal`) tgl,
                            phoneNumber,
                            MIN(TIME( tanggal)) jam_awal,
                            MAX(TIME( tanggal)) jam_akhir,
                            SUM(IF(saran = '1234','1','0')) dinas,
                            SUM(IF(saran = '4321','1','0')) dinastp
                        FROM    t_report 
                        WHERE ( tanggal BETWEEN '$tgl1 00:00:00' AND '$tgl2 23:59:59' )
                        GROUP BY DATE(tanggal),phoneNumber
                     ) tlis
                 ON tlis.phoneNumber = a.`imei_number`
            WHERE a.grup_ID = '$grup'
            $queryIf
            AND (c.user_status !='3' AND c.user_status != '999') 
            GROUP BY a.`imei_number`
            ORDER BY b.nama_tp,a.jabatan

UNION

SELECT  a.nama_account,
             a.jabatan,
             b.nama_tp,
             a.jam_berangkat,
             a.jam_pulang,
             (SUM(IF((jam_awal < jam_berangkat) AND (jam_akhir > '$jampulang_sabtu'),'1','0'))+dinas+dinastp) jum,
             DATEDIFF('$tgl2','$tgl1')+1 jumhar
            FROM t_account a
            LEFT JOIN t_tp b ON a.tp_ID=b.ID
            LEFT JOIN t_user c ON a.userCode=c.userCode
            LEFT JOIN (
                        SELECT  DATE(`tanggal`) tgl,
                            phoneNumber,
                            MIN(TIME( tanggal)) jam_awal,
                            MAX(TIME( tanggal)) jam_akhir,
                            SUM(IF(saran = '1234','1','0')) dinas,
                            SUM(IF(saran = '4321','1','0')) dinastp
                        FROM    t_report 
                        WHERE ( tanggal BETWEEN '$tgl1 00:00:00' AND '$tgl2 23:59:59') AND DAYNAME(tanggal)='Saturday'
                        GROUP BY DATE(tanggal),phoneNumber
                     ) tlis
                 ON tlis.phoneNumber = a.`imei_number`
            WHERE a.grup_ID = '$grup'
            $queryIf
            AND (c.user_status !='3' AND c.user_status != '999') 
            GROUP BY a.`imei_number`
            ORDER BY b.nama_tp,a.jabatan
) AS derivedTable
GROUP BY nama_account,
    jabatan,
    nama_tp,
    jam_berangkat,
    jam_pulang,
    jumhar;

There you go: the combined total of the values of jum from the 2 queries.

Hope this helps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=372112&siteId=1