I need to select a row only from two corresponding tables, for instance,
member table,
member_id member_name
1 xxx
2 yyy
profile_picture table
image_id member_id image_approved
1 1 no
2 1 no
3 1 yes
I want to select one row from member table each time and one approved image from profile_picture. I am using left join but it doesn’t work right as it duplicates the selected member when this member has more than 1 uploaded images. the tricky part is I want to select the image which has been approved only and it is always one picture will be approved.
SELECT *
FROM member
LEFT JOIN profile_picture
ON profile_picture.member_id = member.member_id
WHERE member.member_id = '1'
I would like to get this as a output,
member_id member_name image_id image_approved
1 xxx 3 yes
is it possible?
thanks.
edit:
thanks guys for the suggestions. many of you have suggested using AND – for instance, AND profile_picture.image_approved = ‘yes’
it works only when the image is present. but if the image is absent, there is nothing in the output even though it should output like this below if the image is absent.
member_id member_name image_id image_approved
1 xxx null null
thanks again!
image_approvedisn’t part of the join condition, so I don’t put that in thejoinclause. It’s rather part of the row selection condition, so it goes inwhere. Thinking about what you want, you either want nothing inprofile_picture, or the approved row. So, conditions.