[英]Formatting MySQL query results
let me start off with the fact that i'm still kind of new to php/mysql. 让我从我还是php / mysql的新手开始。
I'm trying to build a system that can be used to record jobs completed for customers that keeps track of job numbers, customer name, equipment, dates, and employee's assigned to the task. 我正在尝试构建一个系统,该系统可用于记录为客户完成的作业,以跟踪作业编号,客户名称,设备,日期和分配给任务的员工。
I have a table setup to handle the employee's assigned, but I can't seem to figure out how to format the results the way I would like. 我有一个表格设置来处理分配给员工的信息,但似乎无法弄清楚如何以自己喜欢的方式格式化结果。
There could be multiple employee's assigned to the same job over several days. 几天内可能有多个员工分配给同一份工作。 So I setup a table like: 所以我建立了一个像这样的表:
job_ID | employee_name | date_worked | hours_worked
1 | Dave | 2010-1-2 | 4.5
1 | Dave | 2010-1-3 | 4.0
1 | Mike | 2010-1-2 | 6.0
2 | Bill | 2011-5-7 | 8.0
2 | Bill | 2011-5-8 | 6.5
2 | Dave | 2011-5-7 | 4.0
2 | Dave | 2011-5-8 | 4.5
What i'm looking to do with the results is output it like: 我想要对结果进行的输出是这样的:
Job # = 1
Employee | 2010-1-2 | 2010-1-3 |
Dave | 4.5 | 4.0 |
Mike | 6.0 | 0 |
Job # = 2
Employee | 2011-5-7 | 2011-5-8 |
Bill | 8.0 | 6.5 |
Dave | 4.0 | 4.5 |
Can anyone offer advice? 谁能提供建议?
EDIT 编辑
Ok, so I guess I left out some information. 好的,所以我想我遗漏了一些信息。
I have a table (job_list) that is used to keep records. 我有一个用于保存记录的表(job_list)。
This table records the job_id, client, equipment, location, call date, response date, completion date, etc. 该表记录job_id,客户,设备,位置,呼叫日期,响应日期,完成日期等。
Then I have a table (employee_list) that lists all employees by id, and name. 然后,我有一个表(employee_list),该表按ID和名称列出了所有员工。
The idea behind the table I posted earlier was to be the table that just records who was where, on what day, and for how long. 我之前发布的表格背后的想法是,该表格仅记录谁在哪里,在哪一天,持续多长时间。
I've amended the table i posted to have employee_id. 我已经修改了我发布的表以拥有employee_id。
And I would like to display the results in php to look like the output i posted. 我想在php中显示结果,使其看起来像我发布的输出。 I just don't know how to do it. 我只是不知道该怎么做。
What other information can I provide? 我还能提供什么其他信息? Apologies for my lack of understanding on what I need to post :-/ 抱歉我对我需要发布的内容缺乏理解:-/
And if my approach is completely horrible, I did say I'm still new to this... 如果我的方法是完全可怕的,我确实说过我对此仍然陌生...
This is basically a PIVOT
but MySQL does not have a PIVOT
function so you will need to replicate it using an aggregate function and a CASE
statement. 这基本上是一个PIVOT
但是MySQL没有PIVOT
函数,因此您将需要使用聚合函数和CASE
语句复制它。 If you know all of the values that you want to transform your query will be similar to this: 如果您知道要转换的所有值,则查询将与此类似:
select employee_name,
sum(case when date_worked = '2010-01-02' then hours_worked else 0 end) `2010-01-02`,
sum(case when date_worked = '2010-01-03' then hours_worked else 0 end) `2010-01-03`
from yourtable
group by employee_name
See SQL Fiddle with Demo 参见带有演示的SQL Fiddle
The result is: 结果是:
| EMPLOYEE_NAME | 2010-01-02 | 2010-01-03 |
-------------------------------------------
| Bill | 0 | 0 |
| Dave | 4.5 | 4 |
| Mike | 6 | 0 |
If you have an unknown number of values, then you can use a prepared statement to generate this dynamically: 如果您有未知数量的值,则可以使用准备好的语句动态生成此值:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(case when date_worked = ''',
date_worked,
''' then hours_worked else 0 end) AS `',
date_worked, '`'
)
) INTO @sql
FROM yourtable;
SET @sql = CONCAT('SELECT employee_name, ', @sql, '
FROM yourtable
GROUP BY employee_name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
See SQL Fiddle with Demo 参见带有演示的SQL Fiddle
Both will produce the same result. 两者都会产生相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.