[英]select data from a table and fill specific value from another table
I have two tables discussions
and votes
that are like this我有两张桌子
discussions
和votes
是这样的
discussions讨论
votes选票
that parentId in votes is id in discussions table投票中的parentId是讨论表中的id
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.我想要一个名为
likeByMe
的键,并希望在为特定用户选择讨论时填写此键。 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并通过 true 或 false 值从投票表中为任何行填充 likeByMe
Given your description, you can use exists
:根据您的描述,您可以使用
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.是您关心的用户的参数占位符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.