使用 NULL 对多个文本列进行排序
Sort multiple text columns with NULL
我有以下列:
a | null
x | f
null | a
i | n
我需要将两列按字母顺序排序,底部为空值,如下所示:
a | a
i | f
x | n
null | null
在 mysql 中是否可以执行此操作?
每一列都必须独立于另一列进行排序,然后按每个值在该顺序中的位置重新组合行。
使用 ROW_NUMBER()
window 函数:
select t1.col1, t2.col2
from (
select col1, row_number() over(order by col1 is null, col1) rn
from tablename
) t1 inner join (
select col2, row_number() over(order by col2 is null, col2) rn
from tablename
) t2 on t2.rn = t1.rn
见 demo.
或者使用 CTE:
with
cte1 as (select col1, row_number() over(order by col1 is null, col1) rn from tablename),
cte2 as (select col2, row_number() over(order by col2 is null, col2) rn from tablename)
select cte1.col1, cte2.col2
from cte1 inner join cte2
on cte2.rn = cte1.rn
参见demo。
结果:
| col1 | col2 |
| ---- | ---- |
| a | a |
| i | f |
| x | n |
| null | null |
要独立地对 2 列进行排序,最后为空值?
对于 MySql 5.x,这里有一个按 π 排序的解:
-- sample data
drop table if exists test;
create table test (col1 varchar(8), col2 varchar(8));
insert into test (col1, col2) values
('a', null), ('x', 'f'), (null, 'a'), ('i', 'n');
-- initiating variables
set @rn1:=0, @rn2:=0;
-- query that links on calculated rownumbers
select col1, col2
from (select @rn1:=@rn1+1 as rn, col1 from test order by coalesce(col1,'π') asc) q1
left join (select @rn2:=@rn2+1 as rn, col2 from test order by coalesce(col2,'π') asc) q2
on q1.rn = q2.rn
order by q1.rn;
结果:
col1 col2
a a
i f
x n
NULL NULL
在 MySql 8.0 中,可以使用 window 函数 ROW_NUMBER
代替变量。
select col1, col2
from (select row_number() over (order by coalesce(col1,'π') asc) as rn, col1 from test) q1
left join (select row_number() over (order by coalesce(col2,'π') asc) as rn, col2 from test where col2 is not null) q2
on q1.rn = q2.rn
order by q1.rn;
对 db<>fiddle here
的测试
我有以下列:
a | null
x | f
null | a
i | n
我需要将两列按字母顺序排序,底部为空值,如下所示:
a | a
i | f
x | n
null | null
在 mysql 中是否可以执行此操作?
每一列都必须独立于另一列进行排序,然后按每个值在该顺序中的位置重新组合行。
使用 ROW_NUMBER()
window 函数:
select t1.col1, t2.col2
from (
select col1, row_number() over(order by col1 is null, col1) rn
from tablename
) t1 inner join (
select col2, row_number() over(order by col2 is null, col2) rn
from tablename
) t2 on t2.rn = t1.rn
见 demo.
或者使用 CTE:
with
cte1 as (select col1, row_number() over(order by col1 is null, col1) rn from tablename),
cte2 as (select col2, row_number() over(order by col2 is null, col2) rn from tablename)
select cte1.col1, cte2.col2
from cte1 inner join cte2
on cte2.rn = cte1.rn
参见demo。
结果:
| col1 | col2 |
| ---- | ---- |
| a | a |
| i | f |
| x | n |
| null | null |
要独立地对 2 列进行排序,最后为空值?
对于 MySql 5.x,这里有一个按 π 排序的解:
-- sample data
drop table if exists test;
create table test (col1 varchar(8), col2 varchar(8));
insert into test (col1, col2) values
('a', null), ('x', 'f'), (null, 'a'), ('i', 'n');
-- initiating variables
set @rn1:=0, @rn2:=0;
-- query that links on calculated rownumbers
select col1, col2
from (select @rn1:=@rn1+1 as rn, col1 from test order by coalesce(col1,'π') asc) q1
left join (select @rn2:=@rn2+1 as rn, col2 from test order by coalesce(col2,'π') asc) q2
on q1.rn = q2.rn
order by q1.rn;
结果:
col1 col2
a a
i f
x n
NULL NULL
在 MySql 8.0 中,可以使用 window 函数 ROW_NUMBER
代替变量。
select col1, col2
from (select row_number() over (order by coalesce(col1,'π') asc) as rn, col1 from test) q1
left join (select row_number() over (order by coalesce(col2,'π') asc) as rn, col2 from test where col2 is not null) q2
on q1.rn = q2.rn
order by q1.rn;
对 db<>fiddle here
的测试