mysql为子查询提供参数

时间:2016-06-19 13:24:30

标签: mysql subquery

我在这里得到了一个很好的复杂mysql查询。

我想要的是,每周,查看在该周内活跃的用户的值的总和。 (活跃用户是加入< = $ week并且离开> $ week)的用户

现在我所做的就是首先列出所有周的清单。 然后提供子选择以获取每周计算的值。

但是,如何为每个子查询提供星期值? 我的例子仅给出1个硬编码周的结果(201444)。 而不是

WHERE yearweek(giveaway.closed) <= 201444

我需要像

这样的东西
WHERE yearweek(giveaway.closed) <= $yearweek

如何填写每一行?

SQL Fiddle

SELECT 
    weeks.yearweek AS 'yearweek',
    COALESCE(valueGiven.valueGiven,0) AS 'valueGiven',
    COALESCE(valueWon.valueWon,0) AS 'valueWon',
    round(valueGiven / valueWon*100,2) AS 'ratio'
FROM 
    (
        select yearweek(selected_date,3) as yearweek
        from 
            (
                select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date 
                from
                (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union
                     select 6 union select 7 union select 8 union select 9) t0,
                (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union
                     select 6 union select 7 union select 8 union select 9) t1,
                (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union
                     select 6 union select 7 union select 8 union select 9) t2,
                (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union
                     select 6 union select 7 union select 8 union select 9) t3,
                (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union
                     select 6 union select 7 union select 8 union select 9) t4
            ) v
        where selected_date between '2014-08-01' and now()
            AND date_format(selected_date, '%w') = 6
        order by yearweek
    ) as weeks

LEFT JOIN (
        SELECT
            '201444' AS yearweekGA,
            sum(giveaway.valuegiven) AS valueGiven
        FROM giveaway

        LEFT JOIN user
            ON user.steamid = giveaway.creatorid

        WHERE yearweek(giveaway.closed) <= 201444
            AND giveaway.winnerid IS NOT NULL
            AND yearweek(user.joined) <= 201444
            AND (user.leftgroup is NULL  OR  yearweek(user.leftgroup) > 201444)
    ) AS valueGiven
    ON weeks.yearweek = valueGiven.yearweekGA

LEFT JOIN (
        SELECT
            '201444' AS yearweekGA,
            sum(giveaway.valuewon) AS valueWon
        FROM giveaway

        LEFT JOIN user
            ON user.steamid = giveaway.winnerid

        WHERE yearweek(giveaway.closed) <= 201444
            AND giveaway.winnerid IS NOT NULL
            AND yearweek(user.joined) <= 201444
            AND (user.leftgroup is NULL  OR  yearweek(user.leftgroup) > 201444)
    ) AS valueWon
    ON weeks.yearweek = valueWon.yearweekGA

ORDER BY weeks.yearweek

预期结果:

201443  2.00    2.00    100.00
201444  2.00    2.00    100.00
...
201515  2.00    3.00    66.67
201516  2.00    3.00    66.67
...
201622  3.00    3.00    100.00

0 个答案:

没有答案