2013-06-26 68 views
0

我希望能夠獲得最近提交的10個補丁(前10個patchID desc) 從下面的查詢開始,我得到10上的錯誤。錯誤是syntax error, unexpected NUM, expecting END_OF_INPUT, or ';'。會在下面的查詢中導致這個錯誤?使用mysql選擇前10個最新的東西

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 

回答

7

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 
+0

太棒了,謝謝你!計時器是3分鐘接受答案 – mhopkins321

+0

謝謝:)我已經添加了一些其他數據庫的例子,以防萬一你覺得它有用 – Wolph