id title slug summary ------------------------------ 1 title1 slug1 summary1 2 title2 slug2 summary2 3 title3 slug3 summary3 4 title4 slug4 summary4
我想选择所有字段,并在此同时,选择ID,标题和上/下一个排的蛞蝓Simplifing MySQL的嵌套SELECT
SELECT
title, slug, summary, id as current_id,
(SELECT id FROM table WHERE id < current_id ORDER BY id DESC LIMIT 1) AS prev_id,
(SELECT title FROM table WHERE id < current_id ORDER BY id DESC LIMIT 1) AS prev_title,
(SELECT slug FROM table WHERE id < current_id ORDER BY id DESC LIMIT 1) AS prev_slug,
/*
(SELECT id ... ) AS next_id
(SELECT title...) AS next_title
...
and if there are more fields to select, I have to repeat this (SELECT...)
*/
FROM
table
WHERE
id IN (2,3,4);
查询作品,但显然它不是聪明的做法。
有些人可以帮助简化这个吗?由于
在SQL中有一个更好的方法 - 只是没有MySQL的有限SQL功能。 –
我们非常渴望向您学习。 – 0xC0DEGURU