簡體   English   中英

MySQL如何從左到右對行進行排序

[英]Mysql how to sort rows from left to right

我有表與字段iddirectionparent_id 有2個值leftright ,我怎么可以命令他們做這樣的事情:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM