![](/img/trans.png)
[英]Select the sum of a column and value of the last column together in a SQL query
[英]My SQL query order so that no same value from the column come together
我正在從多個表中獲取數據,並希望對結果進行排序,以使兩個值不在一起。
例如,查詢返回以下數據。
23
25
26
26
22
22
19
我希望將這種結果排序為這樣,以便沒有兩個值可以使用。
23
25
26
22
26
22
19
您實際上不能保證沒有兩個值會合在一起(例如,所有值可能都相同)。 但是您可以分發它們。 這是使用子查詢和變量的方法:
select t.*
from (select q.*,
(@rn := if(@v = col, @rn + 1,
if(@v := col, 1, 1)
)
) as rn
from (query) q cross join
(select @v := -1, @rn := 0) vars
order by col
) t
order by rn, col;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.