一尘不染

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

mysql

假设我具有以下表格结构:

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

是否可以运行单个查询以结合以下两个条件:

  1. 此人正在关注多少用户

从t1 WHERE userID_follower =“。$ myID”中选择COUNT(id)。。”

  1. 有多少用户关注此人

从t1 WHERE userID_following =“。$ myID”中选择COUNT(id)。

谢谢。


阅读 189

收藏
2020-05-17

共1个答案

一尘不染

在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
2020-05-17