通过已存在的MYSQL查询中的连接来连接两个表

时间:2013-08-01 17:29:46

标签: php mysql

我的结构类似于我的两个表:

z_notes:

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

user_roles:

| 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_id中的

z_notes链接到fk_user_id中的user_roles

因此,应该从查询中排除其角色即i_resident_id == 4的居民(fk_role_id)。也就是说,Where子句中需要有更多内容,例如i_resident_id等于fk_user_id,其表中的列fk_role_id不应为'4'。< / p>

您的意见将受到高度赞赏。 :)

3 个答案:

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

我刚刚说过,添加了一个join和一个where子句

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

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