I have these two tables in mysql database, Can a query be made like this that if there is no data find for the column islike in like_tbl auto make a temp column with value false.
Like there is no data for post_id = 3 in like_tbl so the result should be like this as I mentioned below.
post_tbl
post_id | uid | fname |
---|---|---|
1 | 1 | abc |
2 | 1 | abc |
3 | 1 | abc |
like_tbl
likeid | uid | postid | islike |
---|---|---|---|
1 | 2 | 1 | true |
2 | 3 | 2 | true |
This is the result I'm looking for:
post_id | uid | fname | islike |
---|---|---|---|
1 | 2 | abc | false |
2 | 2 | abc | true |
3 | 2 | abc | false |
Summary I am trying to make a query which will return all post_id and check in like_tbl
that the specific user has liked the post or not. If the data is there for the specific post_id of uid in the like_tbl
it will return islike
column value true otherwise it will return false if no data is found there for that post_id of the specific uid.
I have try this but not helping,
select n.* , e.isLike
from post_tbl n
join like_tbl on e.post_id = n.post_id
where n.uid = '2'
order by post_id desc
New to mysql DB any help would be appreciated
you could try using ifnull and left join
select n.* , ifnull(e.isLike , 'false')
from post_tbl n
LEFT JOIN like_tbl e on e.postid = n.post_id
AND e.uid = n.uid
order by post_id DESC