简体   繁体   English

使用查询结果向结果中添加另一列 SQL

[英]Use result from query to add another column to the results SQL

These are the relevant tables:这些是相关的表格:

  • userInfo用户信息

    • username用户名
    • profileImgUrl profileImgUrl
  • follows跟随

    • follower追随者
    • following下列的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM