php - select data from a table and fill specific value from another table
Get the solution ↓↓↓I have two tablesdiscussions
andvotes
that are like this
discussions
votes
that parentId in votes is id in discussions table
but exist a problem
I want to have a key with namelikeByMe
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
Answer
Solution:
Given your description, you can useexists
:
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.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.