2

我需要将我的主查询与子查询结合起来:

当前查询(有效):

SELECT *,(SELECT SUM(shift_length)FROM overtime_list
WHERE overtime_list.user_ID = users.user_ID AND overtime_list.date > '$totalovertimedays') AS overtime_total FROM availability_list
JOIN users ON users.user_ID = availability_list.user_ID
JOIN stations ON users.station_ID` = stations.station_ID
WHERE availability_list.date = '$date' AND type = '$type'
ORDER BY overtime_total ASC

($date 和 $type 是 PHP 填写的 vars)

我想将其作为子查询插入的查询:

SELECT role_ID, GROUP_CONCAT(users_roles.role_ID) AS roles FROM users
JOIN users_roles ON users.user_ID = users_roles.user_ID
GROUP BY users.users_ID

我走了这么远 - 但我似乎无法让 GROUP BY 工作,所以它没有正确列出它们:

SELECT *,(SELECT SUM(shift_length)FROM overtime_list
WHERE overtime_list.user_ID = users.user_ID AND overtime_list.date > '$totalovertimedays') AS overtime_total FROM availability_list
JOIN users ON users.user_ID = availability_list.user_ID
JOIN `stations` ON `users`.`station_ID` = `stations`.`station_ID`,
(SELECT role_ID, GROUP_CONCAT(users_roles.role_ID separator ', ') AS roles FROM users JOIN users_roles ON users.user_ID = users_roles.user_ID GROUP BY users.user_ID) AS roles
WHERE availability_list.date = '$date' AND type = '$type'
ORDER BY overtime_total ASC

可用性列表表:

 +----------+---------+
 |  user_ID | user    |
 +----------+---------+
 |        1 |   Smith |
 +----------+---------+
 |        2 |   Jones |
 +----------+---------+
 |        3 |   Greg  |
 +----------+---------+

overtime_list 表:

 +----------+----------------+------------+
 |  date    | shift_length   |   user_ID  |
 +----------+----------------+------------+
 |  1/1/11  |   5            |      1     |
 +----------+---------+------+------------+
 |  1/2/11  |   5            |      2     |
 +----------+---------+------+------------+
 |  1/6/11  |   2            |      2     |
 +----------+---------+------+------------+
 |  1/8/11  |   5            |      1     |
 +----------+---------+------+------------+
 |  1/12/11 |   2            |      1     |
 +----------+---------+------+------------+

users_roles 表:

 +----------+---------+
 |  user_ID | roles   |
 +----------+---------+
 |        1 |   Admin |
 +----------+---------+
 |        2 |   Staff |
 +----------+---------+
 |        2 |   Admin |
 +----------+---------+
 |        2 |   Super |
 +----------+---------+
 |        1 |   Other |
 +----------+---------+

所以结果是:

 +----------+---------+----------------------------+------------------+
 |  user_ID | user    |   roles                    |  overtime_total  |
 +----------+---------+----------------------------+------------------+
 |        1 |   Smith |    Admin, Other            |        12        |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Staff, Admin, Super     |        7         |
 +----------+---------+----------------------------+------------------+
 |        3 |   Greg  |                            |  null (or zero)  |
 +----------+---------+----------------------------+------------------+
4

3 回答 3

2

这应该工作 -

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 type = '$type'
GROUP BY users.user_ID
ORDER BY overtime_total ASC
于 2012-04-09T11:22:21.333 回答
0

我建议重新考虑你想要的结果。你要求这个:

 +----------+---------+----------------------------+------------------+
 |  user_ID | user    |   roles                    |  overtime_total  |
 +----------+---------+----------------------------+------------------+
 |        1 |   Smith |    Admin, Other            |        12        |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Staff, Admin, Super     |        7         |
 +----------+---------+----------------------------+------------------+

但是,查询返回以下内容更为常见:

 +----------+---------+----------------------------+------------------+
 |  user_ID | user    |   roles                    |  overtime_total  |
 +----------+---------+----------------------------+------------------+
 |        1 |   Smith |    Admin                   |        12        |
 +----------+---------+----------------------------+------------------+
 |        1 |   Smith |    Other                   |        12        |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Staff                   |        7         |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Admin                   |        7         |
 +----------+---------+----------------------------+------------------+
 |        2 |   Jones |    Super                   |        7         |
 +----------+---------+----------------------------+------------------+

然后将为每个用户创建的行收集在一起非常简单,从而收集他们的所有角色。(如果您先按 排序user_ID,然后检测 user_ID 何时更改,您可能会觉得更容易。)

于 2012-04-09T12:48:51.640 回答
0

如果您坚持使用 将GROUP_CONCAT所有角色名称组合在一起,这可能会起作用:

SELECT users.user_ID, users.user, 
   (SELECT GROUP_CONCAT(users_roles.roles) 
      FROM users_roles
      WHERE users_roles.user_ID = users.user_ID) as roles,
   (SELECT SUM(shift_length)
      FROM overtime_list
      WHERE overtime_list.user_ID = users.user_ID 
        AND overtime_list.date > '$totalovertimedays') AS overtime_total,
   FROM availability_list
     JOIN users ON users.user_ID = availability_list.user_ID
     JOIN stations ON users.station_ID` = stations.station_ID
     WHERE availability_list.date = '$date' AND type = '$type'
     ORDER BY overtime_total ASC

使用这个第二个子查询似乎使这个查询的效率比现在更低,所以我会警惕在大容量系统中使用它。如果您每天只阅读几千篇文章几次,您将永远不会注意到延迟。

于 2012-04-09T13:07:03.657 回答