English 中文(简体)
处理这两份我的SQL询问
原标题:Combine these two mySQL queries

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    |
+----------+------------+
最佳回答

你可以尝试:

SELECT users.*,
        SUM(overtime_list.shift_length) AS overtime_total,
        (SELECT GROUP_CONCAT(roles.short_name) FROM users_roles 
         INNER JOIN roles ON user_roles.role_ID = roles.role_ID
         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
问题回答

添加衍生表格并将其反馈给用户。 由于加班加薪表的总功能,因此数据没有重复。

SELECT users.*,
    SUM(overtime_list.shift_length) AS overtime_total,
    roles.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 
LEFT JOIN
(
   SELECT users_roles.user_ID, 
          GROUP_CONCAT(roles.short_name  separator  ,  ) roles
     from users_roles
      INNER JOIN roles ON users_roles.role_ID = roles.role_ID
    group by users_roles.user_ID
) roles
  ON users.user_ID = roles.user_ID
WHERE availability_list.date =  $date 
AND availability_list.type =  $type 
GROUP BY users.user_ID
ORDER BY overtime_total ASC

或许可以这样说:

SELECT users.*,
        SUM(overtime_list.shift_length) AS overtime_total,
        (SELECT GROUP_CONCAT(users_roles.short_name) 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

我从来不希望利用用户在查询方面的职能,因为数据库引擎将功能作为黑箱处理,并且能够优化内部的问询,从而尽可能避免。 交叉应用后,与意图相同:(didn t test all query as i dont have all Object used)

SELECT users.*, 
        SUM(overtime_list.shift_length) AS overtime_total, 
        LEFT(ISNULL(roles.roles,  ,  ), LEN(ISNULL(roles.roles,  ,  )) - 1) as roles 
    FROM availability_list 
    INNER JOIN users 
        ON users.user_ID = availability_list.user_ID 
    CROSS APPLY (
        SELECT short_name +  ,  
        FROM roles 
        inner users_roles ON roles.role_id = users_roles.role_ID 
        WHERE users.user_ID = users_roles.user_ID 
        ORDER BY roles.role_id
        FOR XML PATH(  )
    ) roles (roles)         
    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 




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签