【发布时间】:2013-05-06 21:39:42
【问题描述】:
所以假设我有这个有 4 列的表:
id content parent timestamp
其中父列引用表中另一个条目的 id
我想完成以下工作:
从表中选择前 50 行,按以下顺序排列:
for each row,
if(parent = 0){
add row to resultset, ordered by timestamp
}
else if (parent != 0){
if parent is in the list of rows already fetched so far by the query,
add row to resultset, ordered by the timestamp
otherwise, wait until the parent gets fetched by the query
(assuming it gets fetched at all since there we're only getting the first 50 rows)
}
这个排序逻辑有点复杂,我想知道是否可以在单个查询中使用 MYSQL ORDER BY 语句来完成这个,而不必求助于子查询?也许我们可以设置和使用变量?但是 ORDER BY 语句将如何实现呢?
【问题讨论】:
-
我很确定
order by不可能做到这一点,即使使用子查询也很难。从树结构中提取子树将具有类似的伪代码。而且,提取子树是一个难题。 -
是一对一的亲子吗?它只有一层吗?
标签: mysql sql select sql-order-by