【发布时间】:2012-11-16 09:54:24
【问题描述】:
id title slug 摘要 ------------------------------ 1 标题1 slug1 摘要1 2 标题2 slug2 摘要2 3 标题3 slug3 摘要3 4 标题4 slug4 摘要4
我正在尝试选择所有字段,同时选择上/下一行的 id、title 和 slug
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 功能。
-
我们渴望向您学习。