Here is one way to do this:
select p.*,
(case when ulp.id_user is not null then 'Y' else 'N' end) as UserLikesPlace
from places p left outer join
user_like_places ulp
on p.id = ulp.id_place and
ulp.id_user = @IDUSER;
It makes the assumption that there are no duplicates in user_like_places
. If that is possible, then you need a distinct
or group by
to remove the duplicates.
The left outer join
keeps all the information about all the places. It then joins in the assocation table. If there is a match, then ulp.id_user
is not null and the user likes the place. Otherwise, it is NULL
and the user does not. If you just want 0 or 1, you can replace the case
statement with ulp.id_user is not null
.