Mysql multiple count query results returned an error, ask for help, thank you

Mysql multiple count query results returned an error, ask for help, thank you

Table structure

  1. Table user (user table) is abbreviated.
  2. Table activity (activity table) is brief.
  3. Table my_calendar_subscribe (user follow activity)

    id (self-added id), tempid (userid), activity (activity id)

  4. Table my_calendar_sign (user joining activity)

    id (self-adding id), tempid (userid), activity (activity id)

Target:

  • query the list of joining activities for a userid, as well as the current number of followers and participants for each activity.

question
query the list of joining activities of a userid and the current number of followers for each of them

select 
        a.activity,
        count(sub.activity) now_sub
from 
        my_calendar_sign a 
left join 
        my_calendar_subscribe sub
on 
        a.activity = sub.activity
where 
        a.tempid = 6
group by 
        a.activity

-result OK

clipboard.png

userid


---OK
clipboard.png

userid


---NOK

clipboard.png

can"t repeat the same field of group by,
, and why is there a problem with only one of the result rows?

ask for help, thank you

Mar.01,2021

you can try this:

select 
        a.activity,
        count(distinct sub.id) now_sub,
        count(distinct s.id) now_sign
from 
        my_calendar_sign a 
left join 
        my_calendar_subscribe sub
on 
        a.activity = sub.activity
left join 
        my_calendar_sign s
on 
        a.activity = s.activity
where 
        a.tempid = 6
group by 
        a.activity
When

count, remember to use fields that uniquely identify my_calendar_subscribe and my_calendar_sign records, for example, do not use activity for their respective id,.

Menu