MySQL使用子查询的结果

时间:2013-08-17 18:27:38

标签: mysql duplicates subquery

我有下一个查询

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)

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

1 个答案:

答案 0 :(得分: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 demonstration