简体   繁体   中英

How to count records from multiple columns Mysql

How to write count mysql query from multiple columns.I have UID which is common in all tables so I framed my query like this It does the job but is there a better way to write multiple count queries

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;
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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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