mohammad reza shabani
mohammad reza shabani

Reputation: 79

select data from a table and fill specific value from another table

I have two tables discussions and votes that are like this

discussions

please see discussions table image

votes

please see votes table image

that parentId in votes is id in discussions table

but exist a problem

I want to have a key with name likeByMe and want to fill this key when select discussions for specific user. how can I connect these two tables and fill this value when call one query?

I want something like this

select * 
from discussions

and fill likeByMe from votes table for any rows by true or false value

Upvotes: 0

Views: 468

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1270873

Given your description, you can use exists:

select d.*,
       (exists (select 1
                from votes v
                where v.parentid = d.id and v.userid = ?
               )
       ) as likedByMe
from discussions d;

The ? is a parameter placeholder for the user you care about.

Upvotes: 0

Related Questions