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
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
select * from actions
left outer join completedactions on ( completedactions.actionid = actions.id and completedactions.userid=1 )
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
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)