MySQL 从结果集中选择TOP 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31583953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:10:07  来源:igfitidea点击:

Select TOP 1 from the result set

mysqlsql

提问by user1894647

I would like to retrieve the top 1 value of result set of query which is connected using Union

我想检索使用 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 

But could not get the expected result.

但是没有得到预期的结果。

回答by Wibbler

There is no TOPkeyword in MySQL as far as I am aware. What you require is Limit:

TOP据我所知,MySQL 中没有关键字。你需要的是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