简体   繁体   English

使用mysql选择十大最新事物

[英]Select top 10 most recent things using mysql

I am hoping to get the ten most recent patches submitted (top 10 patchID desc) Starting with the below query, I am getting an error on the 10 . 我希望获取最近提交的十个补丁(前十个patchID desc),从以下查询开始,我在第10上遇到了错误。 The error is syntax error, unexpected NUM, expecting END_OF_INPUT, or ';' 错误是syntax error, unexpected NUM, expecting END_OF_INPUT, or ';' . Would could be causing this error in the below query? 在以下查询中会导致此错误吗?

SELECT TOP 10 synth.*
FROM (
    SELECT p.`id`, 
        p.`patchName`,
        p.`description`,
        p.`tfsID`,
        p.`codeRelease`,
        s.`name`, 
        d.`deployStatus`, 
        d.`deployedDate`,
        t.`testedStatus`,
        t.`testedDate`
    FROM `patches` AS p 
    JOIN `deployed` AS d ON p.`id` = d.`PatchID`
    JOIN `servers` AS s ON d.`serverID` = s.`id`
    JOIN `tested` AS t ON p.`id` = t.`patchID`
) synth
ORDER BY synth.id DESC

The TOP 10 is a MS SQL specific thing, in MySQL they use LIMIT instead. TOP 10是MS SQL特有的东西,在MySQL中,他们改用LIMIT

SELECT p.`id`, 
    p.`patchName`,
    p.`description`,
    p.`tfsID`,
    p.`codeRelease`,
    s.`name`, 
    d.`deployStatus`, 
    d.`deployedDate`,
    t.`testedStatus`,
    t.`testedDate`
FROM `patches` AS p 
JOIN `deployed` AS d ON p.`id` = d.`PatchID`
JOIN `servers` AS s ON d.`serverID` = s.`id`
JOIN `tested` AS t ON p.`id` = t.`patchID`
ORDER BY p.`id` DESC
LIMIT 10

The syntax for limiting varies quite a bit per server, here's an overview: 每个服务器的限制语法差异很大,这是一个概述:

Starting at row 100 showing 10 results (ie 101, 102, 103...) 从显示10个结果的行100开始(即101、102、103 ...)

PostgreSQL, MySQL and DB2: SELECT * FROM table LIMIT 10 OFFSET 100
Informix: SELECT SKIP 100 FIRST 10 FROM table 
Microsoft SQL Server and Access: SELECT TOP 10 * FROM table -- skipping the offset here as it's a pain, search for it if you need it :)
Oracle: SELECT * FROM (SELECT *, ROW_NUMBER() OVER ORDER BY id) rnk FROM table) WHERE rnk BETWEEN 100 AND 110

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM