Mysql – how to pass outer query param into inner in MySQL 5.7

MySQL

Now I am using this query in MySQL 5.7 to count login count:

select 
(
    select sum(login_count)
    from (
        select avg(login_count) as login_count
        from report_user b
        -- where b.user_id = a.user_id
        group by user_id,date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%d')
    ) as login_count
) as login_count
from report_user a
group by user_id

when I calculate the inner login count, I just want to calculate by per user, then the user_id is specified, the result is correct. But when have many users in this table, the result is not correct. How to pass the outer user id into inner query in execute the query? I want this inner sql only calculate each user:

select sum(login_count)
    from (
        select avg(login_count) as login_count
        from report_user b
        -- where b.user_id = a.user_id
        group by user_id,date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%d')
    ) as login_count

Best Answer

Using this sql solve my problem:

select 
(
    select sum(login_count)
    from (
        select avg(login_count) as login_count,user_id
        from report_user b
        -- where b.user_id = a.user_id
        group by user_id,date_format(FROM_UNIXTIME(statistic_time/1000), '%Y-%m-%d')
    ) as login_count
    where login_count.user_id = a.user_id
) as login_count
from report_user a
group by user_id