2011-02-24 55 views
1

我有一個SQL查詢,當前返回查詢的前50行。SQL查詢,指定哪個頂級結果?

LEFT JOIN users u 
ON c.id = u.id 
ORDER BY c.date DESC LIMIT 50; 

我想改變LIMIT 50是動態的,所以1-24,25-50,等我怎樣才能改變限制50這樣的工作方式,我可以指定結果?

謝謝!

回答

3
ORDER BY c.date DESC LIMIT 0, 25; 

ORDER BY c.date DESC LIMIT 25, 25; 

ORDER BY c.date DESC LIMIT 75, 25; 


$page = 0; // 1, 2, 3 
$perPage = 25; 

$query = ' ... ORDER BY c.date DESC LIMIT ' . ($page * $perPage) . ', ' . $perPage; 
+0

謝謝,沒有的伎倆! – user547794

0

你不能。您只能使用LIMIT檢索連續的行數範圍。

但是,您可以在UNION合併兩個結果放在一起:

(
    SELECT 
    ... 
    LEFT JOIN users u 
    ON c.id = u.id 
    ORDER BY c.date DESC LIMIT 1,25; 
) 
UNION 
(
    SELECT 
    ... 
    LEFT JOIN users u 
    ON c.id = u.id 
    ORDER BY c.date DESC LIMIT 25,26; 
) 
0

如果你不喜歡的是逗號,你可以使用語法

LIMIT 0 OFFSET 25 
LIMIT 25 OFFSET 25