0

我的两个表有一个类似这样的结构:

z_notes:

| i_resident_id | resident_fname | resident_lname | i_communication_log | facility_id | dt_note_created | 

用户角色:

| fk_role_id | fk_user_id | 

旨在在 z_notes 上运行的查询运行良好且非常好。这里是。

"SELECT `i_resident_id` AS ID, resident_fname AS FirstName, `resident_lname` AS LastName, 
                        SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END ) AS Critical_Notes, 
                        SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END ) AS Routine_Notes,
                        SUM(1) AS Total_Notes
                        FROM z_notes
                        WHERE dt_note_created > '$from'
                        AND dt_note_created < '$to' AND
                        facility_id = '$facility_id'
                        GROUP BY v_resident_fname 
                        ORDER BY Total_Notes $asc_des";

这将返回:

ID |    FirstName |     LastName |  Critical_Notes |    Routine_Notes |     Total_Notes

现在这是我需要在上面的查询中加入的地方。外键是这样计算的:

i_resident_idz_notes哪个链接fk_user_iduser_roles

因此,应将i_resident_id角色 ie == 4 的那些居民 ( ) 排除在查询之外。fk_role_id也就是说,在 Where 子句中需要更多内容,应该类似于i_resident_id等于其表中fk_user_id的列fk_role_id不应该是“4”。

您的意见将不胜感激。:)

4

3 回答 3

0

尝试:

SELECT `i_resident_id` AS ID, resident_fname AS FirstName, `resident_lname` AS LastName, 
                        SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END ) AS Critical_Notes, 
                        SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END ) AS Routine_Notes,
                        SUM(1) AS Total_Notes
                        FROM z_notes
                        LEFT JOIN user_roles ON z_notes.i_resident_id = user_roles.fk_user_id
                        WHERE dt_note_created > '$from'
                        AND dt_note_created < '$to'
                        AND facility_id = '$facility_id'
                        AND user_roles.fk_role_id != 4
                        GROUP BY v_resident_fname 
                        ORDER BY Total_Notes $asc_des";

我刚刚你说的,添加了一个连接和一个 where 子句

于 2013-08-01T17:35:12.003 回答
0

尝试这个

SELECT `i_resident_id` AS ID, resident_fname AS FirstName, 
`resident_lname` AS LastName, 
SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END ) AS Critical_Notes, 
SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END ) AS Routine_Notes,
SUM(1) AS Total_Notes
FROM z_notes
LEFT JOIN user_roles ON (z_notes.i_resident_id = user_roles.fk_user_id )
WHERE dt_note_created > '$from'
AND user_roles.fk_role_id !=4
AND dt_note_created < '$to' AND
facility_id = '$facility_id'
GROUP BY v_resident_fname 
ORDER BY Total_Notes $asc_des

或直接将您的 != 置于 ON 子句中

SELECT `i_resident_id` AS ID, resident_fname AS FirstName, 
`resident_lname` AS LastName, 
 SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END ) AS Critical_Notes, 
SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END ) AS Routine_Notes,
SUM(1) AS Total_Notes
FROM z_notes
LEFT JOIN user_roles ON 
(z_notes.i_resident_id = user_roles.fk_user_id  AND user_roles.fk_role_id !=4 )
                        WHERE dt_note_created > '$from'

                        AND dt_note_created < '$to' AND
                        facility_id = '$facility_id'
                        GROUP BY v_resident_fname 
                        ORDER BY Total_Notes $asc_des
于 2013-08-01T17:35:51.957 回答
0

带有子查询的可能解决方案

SELECT `i_resident_id` ID, 
        resident_fname FirstName, 
        `resident_lname` LastName, 
        SUM(CASE i_communication_log WHEN 1 THEN 1 ELSE 0 END ) Critical_Notes, 
        SUM(CASE i_communication_log WHEN 0 THEN 1 ELSE 0 END ) Routine_Notes,
        SUM(1) AS Total_Notes
  FROM z_notes
 WHERE dt_note_created > '$from'
   AND dt_note_created < '$to'
   AND facility_id = '$facility_id'
   AND i_resident_id NOT IN 
       (
         SELECT DISTINCT fk_user_id
           FROM user_roles
          WHERE fk_role_id = 4
       )
 GROUP BY v_resident_fname 
 ORDER BY Total_Notes $asc_des
于 2013-08-01T17:43:49.530 回答