梳理这两个mySQL查询

时间:2012-04-09 10:34:04

标签: mysql subquery

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

当前查询(有效):

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填写的变量)

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

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

availability_list表:

 +----------+---------+
 |  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)  |
 +----------+---------+----------------------------+------------------+

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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何时更改,您可能会觉得更容易。)

答案 2 :(得分: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

使用这个第二个子查询似乎使这个查询的效率甚至低于现有的,所以我要小心在高容量系统中使用它。如果你每天只阅读几千条参赛作品,你就永远不会注意到延迟。