0

我希望获得最近提交的十个补丁(前 10 个补丁ID desc) 从下面的查询开始,我在10. 错误是syntax error, unexpected NUM, expecting END_OF_INPUT, or ';'。在以下查询中可能会导致此错误吗?

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
4

1 回答 1

8

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

每个服务器的限制语法差异很大,这里有一个概述:

从第 100 行开始,显示 10 个结果(即 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
于 2013-06-26T19:03:13.427 回答