我有一个功能可以获取特定ID的记录,并且有两个链接用于上一个和下一个记录。现在我有单独的查询来获取下一个id:
$query = "SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1";
$ getId是当前的ID。
有没有办法整合到一个查询中?
答案 0 :(得分:2)
SELECT A.id AS previd, B.id AS nextid
FROM
(SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 1) A,
(SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1) B
返回2列:previd
和nextid
周围的$ getId
答案 1 :(得分:0)
我认为你可以使用内部查询
SELECT id FROM presentations
WHERE id >= ( SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 0 , 1 )
ORDER BY id ASC LIMIT 0 , 3
第一个数组结果将是前一个记录,第二个将是您当前的记录,最后一个将是您的最后一个记录。