[英]Mysql how to sort rows from left to right
我有表与字段id
, direction
, parent_id
。 有2个值left
和right
,我怎么可以命令他们做这样的事情:
1 row) 1, left, 0
2 row) 2, right, 1
3 row) 3, left, 1
4 row) 4, right, 2
5 row) 5, left, 2
6 row) 6, right, 3
我想根据父代ID从左到右排序,我的意思是每个父代都有2行:左和右。 那可能吗?
您的意思是按parent_id
然后按direction
排序吗?
ORDER BY parent_id, direction
尝试一下...如果每个parent_id都有2个方向,这可能会有所帮助...
select direction , parent_id from
(
select @a:= @a + 1 as a,direction , parent_id from table t,(select @a := 0) a
ORDER BY parent_id , direction
) a
order by case when a = 1 then 1 when a%2 = 0 then a + 1 else a-1 end , parent_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.