过滤group_concat,内连接查询的结果

时间:2011-12-15 22:37:25

标签: php mysql group-concat

新手在这里。下面的代码在返回我想要的结果方面做得很好。唯一的问题是我无法弄清楚如何只返回某个日期范围(在checkdate列中找到)。我可以通过checkdate日期过滤它的唯一方法是GROUP BY empchecks.checkdate并使用HAVING checkdate> = what。但我必须GROUP BY employees.enum。我确定我错过了一些简单的事情。

    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

1 个答案:

答案 0 :(得分:0)

您需要添加WHERE子句。还有一个小修正 - AS checkdate之后的最后一个逗号不应该在那里。

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;