Where clause for only one table in a left join

前端 未结 4 1445
难免孤独
难免孤独 2020-12-21 06:07

I have two tables, the first is \'actions\' which has columns \'id\' and \'actionname\', the second is \'completedactions\' which has \'userid\' and \'actionid\'. They look

相关标签:
4条回答
  • 2020-12-21 06:16
    select 
      actions.id, 
      actions.actionname, 
      count(completedactions.actionid) 
    from actions
    left join completedactions 
      on completedactions.actionid = actions.id and completedactions.userid = 1
    group by 1,2
    
    0 讨论(0)
  • 2020-12-21 06:29

    select * from actions

    left outer join completedactions on ( completedactions.actionid = actions.id and completedactions.userid=1 )

    0 讨论(0)
  • 2020-12-21 06:30

    its will be help full if any body have need to use it.

    select `m`.`id` AS `id`,
    `m`.`CategoryParentId` AS `CategoryParentId`,
    `m`.`CategoryChildId` AS `CategoryChildId`,
    `p`.`CategoryName` AS `ParentCategory`,
    `c`.`CategoryName` AS `ChildCategory`,
    count(c_p.category_id) AS `prodcuts`,
    `img`.`image` AS `CategoryImage`,
    `img2`.`image` AS `thumbImage`
    from (((((`tblcategoryparentchildassociations` `m` 
        left join `tblmastercategories` `c` on((`c`.`id` =  `m`.`CategoryChildId`))) 
        left join `tblmastercategories` `p` on((`p`.`id` = `m`.`CategoryParentId`)))
        left join `vwimagetypeassociations` `img` on((`c`.`id` = `img`.`domainid` AND `img`.`imagetypeid` = 2 AND `img`.`status` = 1 )))
        left join `vwimagetypeassociations` `img2` on((`c`.`id`= `img`.`domainid`AND `img2`.`imagetypeid` = 3 AND `img`.`status` = 1)))
        left join `tblproductcategoriesassociations` `c_p` on((`m`.`CategoryChildId` = `c_p`.`category_id`)))
    group by m.id
    
    0 讨论(0)
  • 2020-12-21 06:42

    Try using your WHERE condition in your ON() clause instead, i.e., ON (c.actionid = a.id AND c.user_id = 1).

    The WHERE filter will apply on the whole resultset, while an additional condition in the join will join the matched results and give null for non-matched results.

    SELECT a.*,c.user_id FROM 
    actions a 
    LEFT JOIN completedactions c 
    ON (c.actionid = a.id AND c.user_id = 1)
    
    0 讨论(0)
提交回复
热议问题