合并单个表中的行会显示重复的结果

时间:2012-05-10 08:49:50

标签: mysql

我正在开发一个简单的PHP和MySQL项目,它具有签入和签出功能。

我在单个表中合并行时遇到一些问题。下面是您可以在不合并行的情况下看到的原始数据。

mysql> SELECT * FROM clock;
+--------+---------+---------------------+----------+
| log_id | user_id | xtimestamp           | text     |
+--------+---------+---------------------+----------+
|      1 | 1000     | 2012-05-10 07:47:06 | CHECKIN  |
|      2 | 1003     | 2012-05-10 07:47:23 | CHECKIN  |
|      3 | 1002     | 2012-05-10 07:47:36 | CHECKIN  |
|      4 | 1002     | 2012-05-10 07:47:49 | CHECKOUT |
|      5 | 1003     | 2012-05-10 07:48:11 | CHECKOUT |
|      6 | 1000     | 2012-05-10 07:48:23 | CHECKOUT |
|      7 | 1000     | 2012-05-10 07:48:52 | CHECKIN  |
+--------+---------+---------------------+----------+

这是我的SQL查询,用于合并单个表上的行以获得签入和签出视图。

SELECT a.log_id as 'ID'
 , a.user_id as 'User_ID'
 , a.xtimestamp as 'CHECKIN'
 , b.xtimestamp as 'CHECKOUT' 
FROM clock a, clock b 
WHERE a.info = 'CHECKIN' 
AND b.info = 'CHECKOUT' 
AND a.user_id = b.user_id 
GROUP BY a.log_id;

这是查询的结果。

+----+---------+---------------------+---------------------+
| ID | User_ID | CHECKIN             | CHECKOUT            |
+----+---------+---------------------+---------------------+
|  1 | 1000    | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 |
|  2 | 1003    | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 |
|  3 | 1002    | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 |
|  7 | 1000    | 2012-05-10 07:48:52 | 2012-05-10 07:48:23 |
+----+---------+---------------------+---------------------+

正如您在上面的结果中看到的,有4条记录返回,但它应该是3,因为在ID 7上,user_id 1000还没有CHECKOUT记录,id 1和id 7具有相同的CHECKOUT日期。

我想要实现的是这样的

+----+---------+---------------------+---------------------+
| ID | User_ID | CHECKIN             | CHECKOUT            |
+----+---------+---------------------+---------------------+
|  1 | 1000    | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 |
|  2 | 1003    | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 |
|  3 | 1002    | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 |
|  7 | 1000    | 2012-05-10 07:48:52 | null                |
+----+---------+---------------------+---------------------+

或者像这样

+----+---------+---------------------+---------------------+
| ID | User_ID | CHECKIN             | CHECKOUT            |
+----+---------+---------------------+---------------------+
|  1 | 1000    | 2012-05-10 07:47:06 | 2012-05-10 07:48:23 |
|  2 | 1003    | 2012-05-10 07:47:23 | 2012-05-10 07:48:11 |
|  3 | 1002    | 2012-05-10 07:47:36 | 2012-05-10 07:47:49 |
+----+---------+---------------------+---------------------+

如果有人有个好主意,请分享..非常感谢。

2 个答案:

答案 0 :(得分:1)

也许是这样的:

SELECT
    MIN(CASE WHEN info='CHECKIN'  THEN xtimestamp ELSE NULL END) AS CHECKIN,
    MAX(CASE WHEN info='CHECKOUT'  THEN xtimestamp ELSE NULL END) AS CHECKOUT,
    MAX(log_id) AS ID,
    User_ID
FROM
    clock
GROUP BY
    User_ID

答案 1 :(得分:1)

SELECT   check_in.log_id           AS `CHECKIN ID`
  ,      check_in.user_id          AS `User_ID`
  ,      check_in.xtimestamp       AS `CHECKIN`
  ,      MIN(check_out.xtimestamp) AS `CHECKOUT` 
FROM     clock                     AS `check_in`
  JOIN   clock                     AS `check_out`
      ON (
           check_out.info = 'CHECKOUT'
       AND  check_in.info = 'CHECKIN'
       AND  check_in.user_id     = check_out.user_id
       AND  check_in.xtimestamp <= check_out.xtimestamp
      )
GROUP BY check_in.log_id, check_in.user_id, check_in.xtimestamp;