从结果集中选择TOP 1

时间:2015-07-23 09:54:51

标签: mysql sql

我想检索使用Union

连接的查询结果集的前1个值
SELECT TOP 1 * FROM
(
    SELECT paused_time as end_time
        FROM production_time
        WHERE created_time = curdate()
    UNION 
    SELECT resumed_time as end_time
        FROM pause_timer
        WHERE created_time = curdate()
    UNION
    SELECT end_time as end_time
        FROM timer_idle
        WHERE created_time = curdate()
) as end_time
ORDER BY end_time DESC 

但无法得到预期的结果。

1 个答案:

答案 0 :(得分:30)

据我所知,MySQL中没有TOP个关键字。您需要的是Limit

SELECT * FROM
(
    SELECT paused_time as end_time FROM production_time WHERE created_time = curdate()
    UNION 
    SELECT resumed_time as end_time FROM  pause_timer WHERE created_time = curdate()
    UNION
    SELECT end_time as end_time FROM  timer_idle WHERE created_time = curdate()
) as end_time
ORDER BY end_time DESC 
LIMIT 1