MySQL:将结果拆分为两个不同的列

时间:2015-08-28 04:51:12

标签: mysql sql

我的结果如下

DATE        EID TIME        TYPE

2015-07-26  1   10:01:00    IN 
2015-07-26  1   15:01:00    OUT 
2015-07-26  1   18:33:00    IN 
2015-07-26  1   23:11:00    OUT

我想将IN,OUT分成不同的列ORDER BY date, eid, time。预期结果应如下

DATE        EID     IN TIME     OUT TIME

2015-07-26  1       10:01:00    15:01:00
2015-07-26  1       18:33:00    23:11:00

这是我到目前为止所尝试的

SELECT `date` AS 'DATE', `eid` AS 'EID',
CASE WHEN `type` = 'IN' THEN `time` END AS 'IN TIME',
CASE WHEN `type` = 'OUT' THEN `time` END AS 'OUT TIME'
FROM `attendance`
ORDER BY `date`, `eid`, `time`;

它取得了如下荒谬的结果

DATE        EID     IN TIME     OUT TIME

2015-07-26  1       10:01:00    null
2015-07-26  1       null        15:01:00
2015-07-26  1       18:33:00    null
2015-07-26  1       null        23:11:00

更新:

这是我的表结构

Field       Type                Null    Key     Default     Extra

id      int(10) unsigned        NO      PRI     NULL        auto_increment
eid     int(10) unsigned        NO              NULL
time    time                    NO              00:00:00
date    date                    NO              0000-00-00
type    enum('IN', 'OUT')       NO              NULL
state   tinyint(1) unsigned     NO              1

这是更多的元组......

DATE        EID TIME        TYPE

2015-07-26  1   10:01:00    IN
2015-07-26  1   15:01:00    OUT
2015-07-26  1   18:33:00    IN
2015-07-26  1   23:11:00    OUT
2015-07-26  3   09:42:00    IN
2015-07-26  3   15:29:00    OUT
2015-07-26  3   18:20:00    IN
2015-07-26  3   00:34:00    OUT
2015-07-26  6   14:16:00    IN
2015-07-26  6   23:08:00    OUT
2015-07-26  8   13:32:00    IN
2015-07-26  8   23:57:00    OUT
2015-07-26  12  09:14:00    IN
2015-07-26  12  15:07:00    OUT
2015-07-26  12  17:28:00    IN
2015-07-26  12  23:53:00    OUT
2015-07-26  13  13:47:00    IN
2015-07-26  13  23:25:00    OUT
2015-07-26  15  11:07:00    IN
2015-07-26  15  19:50:00    OUT

3 个答案:

答案 0 :(得分:2)

我已经为你写了一个查询。我希望它能解决你的问题:

SQL FOR TABLE SCHEMA

CREATE TABLE `attendance` (
  `date` date DEFAULT NULL,
  `eid` int(11) DEFAULT NULL,
  `time` time(6) DEFAULT NULL,
  `type` varchar(5) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

SQL查询结果

我假设您的表格中有一个自动增量列id

SELECT * FROM (
    SELECT a.`date` AS 'DATE', a.`eid` AS 'EID',
        CASE WHEN `type` = 'IN' THEN a.`time` END AS 'IN TIME',
        CASE WHEN `type` = 'IN' THEN 
            (select `time` from `attendance` where id > a.id AND `type` = 'OUT' LIMIT 1) END AS 'OUT TIME'
        FROM `attendance` a
        ORDER BY a.`date`, a.`eid`, a.`time`
) as t WHERE t.`IN TIME` IS NOT NULL;

答案 1 :(得分:1)

假设数据有效(两个IN之间不超过一个OUT),使用窗口函数:

SELECT DATE, EID, `IN`, `OUT` FROM 
  (SELECT date AS 'DATE', eid AS 'EID', 
     (SELECT MAX(TIME) from attendance  where
            DATE = a.DATE AND TIME <= a.TIME AND TYPE = 'IN' AND EID = a.EID) as 'IN',
     (SELECT MAX(TIME) from attendance  where
            DATE = a.DATE AND TIME <= a.TIME AND TYPE = 'OUT' AND EID = a.EID) as 'OUT',
      TYPE           
   FROM attendance a
   WHERE TYPE = 'OUT'
   ORDER BY date, eid, time) t

这是一个fiddle

修改:我没有查看EID,这是新的fiddle

答案 2 :(得分:0)

我已经添加了另一个答案,只是为了解决Taher所说的问题:

<强>查询

SELECT * FROM(
SELECT a.`date` AS 'DATE', a.`eid` AS 'EID',
    CASE WHEN `type` = 'IN' THEN a.`time` END AS 'IN TIME',
    CASE WHEN `type` = 'IN' THEN 
        (select `time` from
            (SELECT `date`, `eid`, `time`,`type`, @rownum := @rownum + 1 as id FROM `attendance` cross join (select @rownum := 0) r ORDER BY `date`, `eid`, `time`) as b where (b.id > a.id) AND `type` = 'OUT' LIMIT 1)
         END AS 'OUT TIME'

    FROM(SELECT `date`, `eid`, `time`,`type`, @rownum := @rownum + 1 as id FROM `attendance` cross join (select @rownum := 0) r ORDER BY `date`, `eid`, `time`) as a
) as t WHERE t.`IN TIME` IS NOT NULL;