3

I have three tables. I am trying to write a query for that

I have written two Queries for that

1)

SELECT  
   `user_favourite_bookmarks`.`user_bookmark_id` as `is_fav`,
   `user_bookmarks`.* FROM `user_favourite_bookmarks` 
     RIGHT JOIN 
    `user_bookmarks` ON `user_favourite_bookmarks`.`user_bookmark_id` =    `user_bookmarks`.`bookmark_id` 
   WHERE 
 `user_bookmarks`.`user_id`=26 
 group by `user_bookmarks`.`bookmark_id`
 order by created_at DESC

and second query below

2)

 SELECT  `user_deleted_bookmarks`.`bookmark_id` as `is_deleted`,`user_bookmarks`.*, FROM `user_deleted_bookmarks`
RIGHT JOIN 
**`user_bookmarks` ON `user_deleted_bookmarks`.`bookmark_id` = `user_bookmarks`.`bookmark_id` 
WHERE 
`user_bookmarks`.`user_id`=26 
group by `user_bookmarks`.`bookmark_id` 
order by created_at DESC

These Queries are running Accurate

now my first query gives me out like is_fav, title and second one

is_deleted, title

But i want result something like that is_fav, is_deleted, title

Anybody tell me how to achieve this result. I want to achieve this result in mysql .

Thanks

1 Answer 1

2

Merge those two queries into one query.

SELECT
  b.title,
  IF(f.bookmark_id IS NULL, 'no', 'yes') AS 'favorite',
  IF(d.bookmark_id IS NULL, 'no', 'yes') AS 'deleted'

FROM user_bookmarks b

LEFT JOIN user_deleted_bookmarks d
ON d.bookmark_id = b.bookmark_id

LEFT JOIN user_favourite_bookmark f
ON f.bookmark_id = b.bookmark_id

WHERE b.user_id = 26

GROUP BY b.bookmark_id
ORDER BY b.created_at DESC
Sign up to request clarification or add additional context in comments.

3 Comments

there is not such field like is_fav, Please read my query carefully
@Hitu Sorry Sir, I updated the answer in respect to your avail columns!
Can you tell me how to get records if deleted is no only from above statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.