duanliao6077 2018-06-13 08:25
浏览 76
已采纳

如何显示表结果列并在行上显示它

enter image description here

Can someone help me create a table in where I can use the other rows as columns?

My sample database looks like this:

|    NAME | DATE             |    TIME     |TYPE |
|    john    | 03-05-2017  |    06:00    | AM IN
|    john    | 03-05-2017  |    11:00    | AM OUT
|    john    | 03-05-2017  |    13:00    | PM IN
|    john    | 03-05-2017  |    16:00    | PM OUT

| john | 03-06-2017  | 06:00 | AM IN
| john | 03-06-2017  | 11:00 | AM OUT

| john | 03-07-2017  | 06:00 | AM IN
| john | 03-07-2017  | 11:00 | AM OUT
| john | 03-07-2017  | 13:00 | PM IN
| john | 03-07-2017  | 16:00 | PM OUT

How can I show it like this in PHP table:

|    DATE      | AM IN  |AM OUT   |    PM IN    | PMOUT
03-05-2017   06:00     |11:00        |   13:00      |16:00
03-06-2017   06:00     |11:00        |   NULL       | NULL
03-07-2017   06:00    |11:00         |    13:00      |16:00

I have uploaded the image so that I can explain it better

  • 写回答

1条回答 默认 最新

  • duanchi5078 2018-06-13 08:33
    关注

    I think you just want a pivot query:

    SELECT
        NAME,
        DATE,
        MAX(CASE WHEN TYPE = 'AM IN'  THEN TIME END) AS AM_IN,
        MAX(CASE WHEN TYPE = 'AM OUT' THEN TIME END) AS AM_OUT,
        MAX(CASE WHEN TYPE = 'PM IN'  THEN TIME END) AS PM_IN,
        MAX(CASE WHEN TYPE = 'PM OUT' THEN TIME END) AS PM_OUT
    FROM yourTable
    GROUP BY
        NAME, DATE;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含