我有一个mysql表名employee。我想显示所有结果,但需要将每个部门名称显示为标题
我尝试了如下所示的php代码,我知道它不会显示我所需的结果,但我不知道。
$result = mysqli_query($mysqli, "SELECT * FROM employee GROUP BY department , id ORDER BY name ASC");
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['salary']."</td>";
echo "<td>".$res['department']."</td>";
}
我的查询显示在结果下方
Name Salary Department
A 10 M
B 10 M
C 10 M
D 10 X
F 10 X
G 10 T
H 10 T
I 10 T
K 20 L
F 20 L
G 20 L
I 20 L
但是我想得到如下结果
Name | Salary
----------------
Department : M
---------------
A | 10
---------------
B | 10
---------------
C | 10
---------------
Department : X
---------------
D | 10
---------------
F | 10
---------------
Department : T
---------------
G | 10
---------------
H | 10
---------------
I | 10
---------------
Department : L
---------------
K | 20
---------------
F | 20
---------------
G | 20
---------------
I | 20
---------------
那么可以通过使用php吗?