简体   繁体   English

从表中选择数据并从另一个表中填充特定值

[英]select data from a table and fill specific value from another table

I have two tables discussions and votes that are like this我有两张桌子discussionsvotes是这样的

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.

相关问题 Mysql从特定值开始选择和获取表数据 - Mysql Select and Fetch table data starting from specific value 从一个表中选择所有行,并为另一表中的每一行选择一个特定值 - Select all rows from one table and one specific value for each row from another table 表查询,select 数据来自 table1,但前提是 table2 中的用户设置设置为特定值 - Table query, select data from table1, but only if user settings in table2 is set to a specific value Select 表中的数据使用另一个表中的数据 - Select data from table using data from another table 如何通过用户操作从一个表中选择特定数据并将其一部分重新插入另一表 - How to select specific data from one table and insert a part of it back to another table by user action 从一个表中选择不在另一表中但具有特定条件的记录 - Select records from one table that are not in another table but with specific conditions 如何从另一个表中选择表中的特定条件 - how to select a specific condition in table from another table MySQL从不在另一个表中的表中选择特定条目 - MySQL select specific entry from a table which is not in another table 努力从另一个表中选择一个值 - Struggling to select a value from another table 从另一个表中选择计数更改值 - select count change value from another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM