0

我是mysql查询的新手,我想问一下,是否有人可以帮助我解决我的问题。我有这个复杂的(对我来说)sql。我想把它变成一个视图。

SELECT 
    username, 
    user_id,
    sum(result_points) AS count_points, 
    count(result_points) AS activity_done, 
    (
        (
            SELECT count(*) FROM `activity` WHERE periode_id = ''
        ) + 
        (
            SELECT
                IFNULL(sum(acs.result_points), 0) 
            FROM 
                user_activity ua 
            WHERE 
                periode_id = '' 
            AND ua.user_id = user_activity.user_id
            AND ua.result_points IS NOT NULL
        )
    ) AS total
    FROM 
        user_activity 
        LEFT JOIN users ON users.id = user_activity.user_id 
    WHERE 
        periode_id = '' 
    and user_id > 0 
    GROUP BY user_id 
    ORDER BY total DESC

有没有办法取出“where”语句,以便我仍然可以将其更改为查看?

4

1 回答 1

0

如果您的查询是正确的,请创建类似的视图

Create View data as
SELECT 
    username, 
    user_id,
    sum(result_points) AS count_points, 
    count(result_points) AS activity_done, 
    (
        (
            SELECT count(*) FROM `activity` WHERE periode_id = ''
        ) + 
        (
            SELECT
                IFNULL(sum(acs.result_points), 0) 
            FROM 
                user_activity ua 
            WHERE 
                periode_id = '' 
            AND ua.user_id = user_activity.user_id
            AND ua.result_points IS NOT NULL
        )
    ) AS total
    FROM 
        user_activity 
        LEFT JOIN users ON users.id = user_activity.user_id 
    WHERE 
        periode_id = '' 
    and user_id > 0 
    GROUP BY user_id 
    ORDER BY total DESC

从视图中删除 where 子句并在视图中添加 where 子句,例如

select * from data where user_id > 0 
于 2012-10-27T12:36:11.440 回答