使用php从Mysql显示数据

时间:2013-11-27 10:30:07

标签: php html mysql

我的mySql数据库中有数据,表结构如下,

   e_id | p_name | time | taskdate
   VT01 |   p1   | 2    | 2013-11-22
   VT01 |   p1   |  1   | 2013-11-23
   VT02 |   P2   |  3   | 2013-11-23
   VT01 |   P3   |  2   | 2013-11-24

E_id适用于不同的员工。用户输入员工ID,并根据该ID,我应该从mysql中提取数据并以下列方式显示它。我有一周选择,选择一周。例如。选择员工VT01并选择一周

    P_name | S | M | TU | W | TH | F | S
      P1   | 2 | 1 |    |   |    |   |  
      P2   |   |   | 2  |   |    |   |

我应该如何查询数据库以及如何在php中显示它

1 个答案:

答案 0 :(得分:1)

  • 首先使用DAYOFWEEK()来获取一周中的特定日期。
  • 接下来,您应该按p_name分组。
  • 最后一个你要创建所谓的cross tab

一起你会得到

SELECT
  p_name,
  SUM(IF(DAYOFWEEK(taskdate) = 1, `time`, 0)) AS `su`,
  SUM(IF(DAYOFWEEK(taskdate) = 2, `time`, 0)) AS `mo`,
  SUM(IF(DAYOFWEEK(taskdate) = 3, `time`, 0)) AS `tu`,
  SUM(IF(DAYOFWEEK(taskdate) = 4, `time`, 0)) AS `we`,
  SUM(IF(DAYOFWEEK(taskdate) = 5, `time`, 0)) AS `th`,
  SUM(IF(DAYOFWEEK(taskdate) = 6, `time`, 0)) AS `fr`,
  SUM(IF(DAYOFWEEK(taskdate) = 7, `time`, 0)) AS `sa`
FROM mytable
WHERE e_id = 'VT01'
GROUP BY p_name WITH ROLLUP

See this fiddle