www.un.org/spanish/ecosoc 回答:
SELECT users.*,
SUM(overtime_list.shift_length) AS overtime_total,
(SELECT GROUP_CONCAT(users_roles.role_ID) FROM users_roles WHERE users.user_ID = users_roles.user_ID) AS roles
FROM availability_list
INNER JOIN users
ON users.user_ID = availability_list.user_ID
INNER JOIN stations
ON users.station_ID = stations.station_ID
INNER JOIN overtime_list
ON overtime_list.user_ID = users.user_ID
AND overtime_list.date >= $totalovertimedays
WHERE availability_list.date = $date
AND availability_list.type = $type
GROUP BY users.user_ID
ORDER BY overtime_total ASC
<>光>
+----------+---------+----------------------------+------------------+
| user_ID | user | roles | overtime_total |
+----------+---------+----------------------------+------------------+
| 1 | Smith | 1,2 | 12 |
+----------+---------+----------------------------+------------------+
| 2 | Jones | 1,2,3 | 7 |
+----------+---------+----------------------------+------------------+
www.un.org/spanish/ecosoc 这是预期的结果:。
+----------+---------+----------------------------+------------------+
| user_ID | user | roles | overtime_total |
+----------+---------+----------------------------+------------------+
| 1 | Smith | Admin, Staff | 12 |
+----------+---------+----------------------------+------------------+
| 2 | Jones | Admin, Staff, Other | 7 |
+----------+---------+----------------------------+------------------+
www.un.org/spanish/ecosoc 这是我可以使用的一种做法,这似乎使专家组能够纠正加入“行政、工作人员和其他”的情况,但我必须研究如何将其纳入上述主要问题?。
SELECT users.user_id, GROUP_CONCAT(roles.short_name separator , ) roles
FROM users
JOIN users_roles ON users.user_ID = users_roles.user_ID
JOIN roles ON users_roles.role_ID= users_roles.role_ID
GROUP BY users.user_ID
www.un.org/spanish/ecosoc 用户_名册:
+----------+---------+
| user_ID | role_ID |
+----------+---------+
| 1 | 1 |
+----------+---------+
| 2 | 1 |
+----------+---------+
| 2 | 2 |
+----------+---------+
| 2 | 3 |
+----------+---------+
| 1 | 3 |
+----------+---------+
www.un.org/spanish/ecosoc 角色表:
+----------+------------+
| role_ID | short_name |
+----------+------------+
| 1 | Admin |
+----------+------------+
| 2 | Super |
+----------+------------+
| 3 | Other |
+----------+------------+