简体   繁体   中英

filtering results of group_concat, inner join query

Newbie here. The code below is doing a great job returning the results I want. The only problem is that I can't figure out how to return only a certain date range (found in checkdate column). The only way I can get it to filter by date of checkdate is to GROUP BY empchecks.checkdate and use HAVING checkdate>=whatever. But I have to GROUP BY employees.enum. I'm sure I'm missing something simple.

    SELECT employee_data.a, 
           employees_data.b, employee_data.c, 
           GROUP_CONCAT(empchecks.d 
               ORDER BY empchecks.checkdate 
              SEPARATOR '<br /> ') 
           AS checknum, 
           GROUP_CONCAT(empchecks.checkdate 
               ORDER BY empchecks.checkdate      
              SEPARATOR '<br /> ') 
           AS checkdate, 
      FROM employees 
INNER JOIN empchecks ON employees.enum = empchecks.enum 
  GROUP BY empchecks.enum

You need to add a WHERE clause. Also a minor correction - that last comma after AS checkdate should not be there.

SELECT employee_data.a, employees_data.b, employee_data.c, 
       GROUP_CONCAT(empchecks.d ORDER BY empchecks.checkdate SEPARATOR '<br /> ') as checknum, 
       GROUP_CONCAT(empchecks.checkdate ORDER BY empchecks.checkdate SEPARATOR '<br /> ') as checkdate
    FROM employees 
        INNER JOIN empchecks 
            ON employees.enum = empchecks.enum 
    WHERE empchecks.checkdate >= @begin_date
      AND empchecks.checkdate <= @end_date
    GROUP BY empchecks.enum;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM