[英]Use result from query to add another column to the results SQL
These are the relevant tables:这些是相关的表格:
userInfo用户信息
follows跟随
In the follows table the follower and the following attributes are usernames.在下表中,关注者和以下属性是用户名。 Where the follower follows the following.
追随者遵循以下内容。
I would like to add another column where it states if the person requesting the query, in this case, 'admin', follows the selected u.username.我想添加另一列,其中说明请求查询的人(在本例中为“管理员”)是否遵循所选的 u.username。
Current query:当前查询:
SELECT u.username, u.profileImgUrl
FROM userInfo u, follows f WHERE f.following = 'admin'
AND f.follower = u.username
I think you want:我想你想要:
select u.*,
( exists (select 1 from follows f where f.follower = u.username and f.following = 'admin')
) as flag
from userInfo u;
MySQL supports a "boolean" data type that can look a lot like a number, so this returns "1" if a row exists in followers
and 0
if it does not. MySQL 支持“布尔”数据类型,它看起来很像一个数字,因此如果
followers
中存在一行,则返回“1”,否则返回0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.