如何统计多列记录

nkhmeac6  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(287)

如何从多个列中编写count mysql查询。我有uid,这在所有表中都是通用的,所以我把我的查询框成这样它可以完成工作,但是有更好的方法来编写多个count查询吗

SELECT
  (SELECT count(*)  from follow WHERE followed_user_uid = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') as following,
  (SELECT count(*)  from follow WHERE  my_user_uid= 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') as followers,
  SUM(
      (SELECT count(*) from prac_test where UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') +
      (select count(*) from multi_test where my_UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') +
      (select count(*) from shadow_test where UID = 'b4eb3820-1fc6-11e8-aead-23ee40fdc27f')
  ) as totalTestCount;
xlpyo6sf

xlpyo6sf1#

SELECT * FROM
    (SELECT 
        (SELECT count(*)  from follow AS flw WHERE flw.followed_user_uid = param.user_id) as following,
        (SELECT count(*)  from follow AS flw WHERE flw.my_user_uid= param.user_id) as followers,
        SUM(
          (SELECT count(*) from prac_test AS pt where pt.UID = param.user_id) +
          (select count(*) from multi_test AS mt where mt.my_UID = param.user_id) +
          (select count(*) from shadow_test AS st where st.UID = param.user_id)
        ) AS totalTestCount
    FROM
        (SELECT
                'b4eb3820-1fc6-11e8-aead-23ee40fdc27f') AS user_id,
        ) AS param
    )AS tmp;

相关问题