duanfan8360 2013-12-26 02:14
浏览 22

如何添加逗号分隔的备注列? 选择查询混乱

I have 3 pairs of in/out employee records existing code.. but from the my table I need the remarks on each and every input be seen in one cell with comma separated.

Please see my fiddle.. http://sqlfiddle.com/#!2/afe72/1/0

My table will be like this..

create table overallrec(
    td_id int auto_increment primary key,
    emp_id int,  
    status varchar(3),
    timestamp datetime,
    remarks varchar(255)
);

insert into overallrec(emp_id,status,timestamp) values  
 ( 35, 'in' , '2013-12-19 10:15:09', 'late'),
 ( 35, 'out', '2013-12-19 12:00:23', 'example'),
 ( 35, 'in' , '2013-12-19 13:00:23'),
 ( 35, 'out', '2013-12-19 16:01:47'),
 ( 35, 'in' , '2013-12-19 18:01:17'),
 ( 35, 'out', '2013-12-19 22:01:07'),
 ( 35, 'in' , '2013-12-20 10:00:12'),
 ( 36, 'in' , '2013-12-18 10:15:09'),
 ( 36, 'out', '2013-12-18 12:00:23'),
 ( 37, 'in' , '2013-12-17 10:15:09'),
 ( 37, 'out', '2013-12-17 12:00:23'),
 ( 37, 'in' , '2013-12-17 13:00:23');

I want it to have a remarks at the end of each row with a comma separated strings. like this(reference 1st row from my fiddle):

35  in  December, 19 2013 10:15:09+0000
out December, 19 2013 12:00:23+0000
in  December, 19 2013 13:00:23+0000
out December, 19 2013 16:01:47+0000
in  December, 19 2013 18:01:17+0000
out December, 19 2013 22:01:07+0000      late, example

Please help me.. I'm hoping for an answer as a Christmas gift.. :/

  • 写回答

2条回答 默认 最新

  • dua55014 2013-12-26 02:39
    关注

    GROUP_CONCAT can probably give you what you need.

    Try this query to give you one resultset row per day per employee.

    SELECT emp_id,
           GROUP_CONCAT(CONCAT(status, ' ', timestamp)) AS log,
           GROUP_CONCAT(remarks) AS remarks
      FROM overallrec
     GROUP BY emp_id, DATE_FORMAT(timestamp,'%Y-%m-%d')
     ORDER BY DATE_FORMAT(timestamp,'%Y-%m-%d'), emp_id
    

    I didn't format your timestamps the same way you did, but other than that I think this is close. Here's a fiddle. http://sqlfiddle.com/#!2/d7755/5/0

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?