简体   繁体   中英

Where clause for only one table in a left join

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 as follows:

Table: actions

 id | actionname
 ==============
  1 | An action
  2 | Another
  3 | ect
  4 | ect

actions is a table of actions a user can make

-

Table: completedactions

 userid | actionid
 ==============
   1    |  1
   1    |  2
   1    |  3
   2    |  3
   3    |  2
   3    |  4

completedactions holds the userid and action id for each action made

I want to join the tables to give a list of all actions and which of those actions have been made by a given user, specified somewhere in the query. So from this example I'd like to see something like:

 id | actionname | Complete by userid=1?
 =======================================
  1 | An action  | 1
  2 | Another    | 1
  3 | ect        | 1
  4 | ect        | Null

I've tried a left join and have managed to get a list of all actions but there are duplicate entries when multiple users have taken an action, one for each user to have made the action. I then added a where clause at the end compledtedactions.userid="1" but I then lose all actions that haven't been made by that user.

I can't seem to have a where clause for only one table in the left join so how else should I be going about this?

Try using your WHERE condition in your ON() clause instead, ie, 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)
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM