Skip to content
Advertisement

how to get single result in using 2 table data

i have to table 2 in same structure .i already get the result by using one table using following query

SELECT *,COUNT(Time), 
             CASE WHEN COUNT(Time) = 1 
                 THEN CASE WHEN Time > '00:00:00' && Time <= '12:15:00' 
                 THEN Time END ELSE MIN(Time) 
                 END as time_in, 
                 CASE WHEN COUNT(Time) = 1 
                     THEN CASE WHEN Time > '12:15:00' && Time <= '23:59:59' 
                     THEN Time END ELSE NULLIF(MAX(Time), MIN(Time)) 
                     END as time_out 
                 FROM attendancedata 
                 INNER JOIN nonacadamic 
                 ON attendancedata.EnrolledID = nonacadamic.emp_id 
                 WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%' 
                 GROUP BY EnrolledID,Date

this query will time devide in to the 2 part(time in and time out) .it work fine.now want to get the data from anther table also.it also have same structure attendancedata table.

attendancedata table structure

EnrolledID  Date    Time    
23  2019-09-09  16:19:00    
53  2019-08-27  08:19:00

tempattendancedata table structure

EnrolledID  Date    Time    
23  2019-09-09  16:19:00    
23  2019-09-20  08:19:00

i want get the result consider above table record and then split time in to the two part .how can i do this task? actual requirement is tempattendancedata table data also need considering for the time split

Advertisement

Answer

you include the results of your temp table using UNION ALL then, put it in a subquery for your select case statement

SELECT *,COUNT(Time)
    ,CASE WHEN COUNT(Time) = 1 THEN 
        CASE WHEN Time > '00:00:00' && Time <= '12:15:00' 
            THEN Time END ELSE MIN(Time) 
    END as time_in, 
    CASE WHEN COUNT(Time) = 1 THEN 
        CASE WHEN Time > '12:15:00' && Time <= '23:59:59' 
            THEN Time END ELSE NULLIF(MAX(Time), MIN(Time)) 
    END as time_out 
FROM
    (SELECT *
    FROM attendancedata 
    INNER JOIN nonacadamic 
    ON attendancedata.EnrolledID = nonacadamic.emp_id 
    WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%'    
    UNION ALL
    SELECT *
    FROM tempattendancedata 
    INNER JOIN nonacadamic 
    ON attendancedata.EnrolledID = nonacadamic.emp_id 
    WHERE nonacadamic.emp_id = '".$_POST["emp_id"]."' AND Date LIKE '$currentMonth%') t1
GROUP BY t1.EnrolledID, t1.Date
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement