gpt4 book ai didi

mysql - 是否可以在同一个查询中计算两列

转载 作者:IT老高 更新时间:2023-10-29 00:08:39 27 4
gpt4 key购买 nike

假设我有以下表结构:

t1
-------------
id // row id
userID_follower // this user is a follows another member
userID_following // other member that this user

是否可以运行单个查询来结合以下两者:

  1. 此人关注了多少用户

    select COUNT(id) from t1 WHERE userID_follower = ".$myID." ."

  2. 有多少用户关注此人

    select COUNT(id) from t1 WHERE userID_following = ".$myID."

谢谢。

最佳答案

在 MySql 中,您可以在条件上使用 SUM() 函数,因为假条件将等于 0,而真条件将等于 1:

SELECT SUM(userID_follower = $myID) AS followerCount,
SUM(userID_following = $myID) AS followingCount
FROM t1
WHERE userID_follower = $myID
OR userID_following = $myID

关于mysql - 是否可以在同一个查询中计算两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5133388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com