how to use sql join in mysql

前端 未结 4 1087
礼貌的吻别
礼貌的吻别 2021-01-23 07:33

table name: tbl_schedule

tr_id(P.K.)  mr_id(F.K.)   sch_date   doctor_id   
-----------  -----------   --------   ----------
1              23         01/01/2012         


        
4条回答
  •  醉话见心
    2021-01-23 08:17

    You can try this

    SELECT b.mr_id, b.mr_fname, b.mr_lname, 
         (SELECT COUNT(*) FROM tbl_schedule a WHERE (a.my_id = b.my_id)
         AND (Date >= '2012-04-01') AND (Date <= '2012-04-20')) AS COUNT
    FROM tbl_user
    

    You do not need to INNER JOIN, because it seems like you want to include ALL of the users in the tbl_users table.

    Edit: You added later that you do not want a count of 0

    SELECT b.mr_id, b.mr_fname, b.mr_lname, COUNT(*)
    FROM tbl_schedule a 
    INNER JOIN tbl_users b ON a.tr_id = b.mr_id
    WHERE (Date >= '2012-04-01') AND (Date <= '2012-04-20')
    GROUP BY b.mr_id, b.mr_fname, b.mr_lname
    

提交回复
热议问题