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