1

我有下一个查询

SELECT * 
FROM(
    SELECT 
        ID,
        SUM(points) AS SUMMARY
    FROM my_table
    GROUP BY ID
    ORDER BY SUMMARY DESC
) t1
WHERE SUMMARY>=(SELECT 
                    SUMMARY
                FROM (
        SELECT 
                        ID,
                        SUM(points) AS SUMMARY
        FROM my_table
        GROUP BY ID
        ORDER BY SUMMARY DESC
        ) t2
    WHERE ID=1234)

如何删除重复查询或重用选择结果?也许我的要求完全不正确?

4

1 回答 1

1

我很确定您的查询与以下内容相同:

SELECT ID, SUM(points) AS SUMMARY
FROM my_table
GROUP BY ID
HAVING SUMMARY >= (SELECT SUM(points) FROM my_table WHERE ID=1234)
ORDER BY SUMMARY DESC

SQL Fiddle 演示

于 2013-08-17T18:33:20.260 回答