[英]How to write a mysql query to get 2 dimensional table in php?
I am trying to output a 2D table in php but not able to figure out the sql query for the same 我正在尝试在php中输出2D表,但无法找出相同的sql查询
My tables are as below 我的桌子如下
Students
========
ID | Name
1 | Student1
2 | Student2
3 | Student3
Projects
========
ID | Name
1 | Project1
2 | Project2
3 | Project3
Timesheet
========
ID | Project_id | Student_id | hours
1 | 1 | 1 | 2.5
2 | 2 | 2 | 3
3 | 2 | 1 | 4.5
4 | 1 | 3 | 5
5 | 3 | 3 | 2
6 | 3 | 2 | 1
7 | 3 | 3 | 3.5
8 | 2 | 1 | 6
I want the output as below 我想要以下输出
| Student1 | Student2 | Student3
Project1 | 2.5 | 0 | 5
Project2 | 10.5 | 3 | 0
Project3 | 0 | 1 | 5.5
Number of students and project will increase and not constant. 学生人数和项目数量会增加,并且不会保持恒定。 Is it possible to write in one sql join query or do I have to write multiple nested php loops to get the above output? 是否可以在一个sql联接查询中编写,还是我必须编写多个嵌套的php循环才能获得上述输出?
For a big dataset, complex queries using multiple JOINs are basically not good. 对于大型数据集,使用多个JOIN的复杂查询基本上不好。
"SELECT SUM(hours) AS total FROM Timesheet GROUP BY Project_id, Student_id"
"SELECT ID,Name FROM Students"
"SELECT ID,Name FROM Projects"
I'd rather execute 3 simple queries, then match student and project names in a single loop 我宁愿执行3个简单查询,然后在单个循环中匹配学生和项目名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.