douzhang7603 2013-08-01 17:29
浏览 24
已采纳

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

I have a structure something like this of my two tables:

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 | 

The query that is meant to run on z_notes is working fine and perfectly well. Here it is.

"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";

This returns:

ID |    FirstName |     LastName |  Critical_Notes |    Routine_Notes |     Total_Notes

Now this is where i need a join in the query above. This is how the foreign key is worked out:

i_resident_id in z_notes which links to fk_user_id in user_roles

Hence, those residents (i_resident_id) whose role i.e. fk_role_id == 4 should be excluded from the query. That is, There needs to be more in Where clause which should be something like i_resident_id which is equal to fk_user_id has a column fk_role_id in its table which should not be '4'.

Your input will be highly appreciated. :)

  • 写回答

3条回答 默认 最新

  • doufei1852 2013-08-01 17:35
    关注

    Try:

    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";
    

    I just did you said, added a join and a where clause

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)