gpt4 book ai didi

mysql - 对选定行中的列值进行排序

转载 作者:可可西里 更新时间:2023-11-01 08:10:02 25 4
gpt4 key购买 nike

我有一个表,其中有 4 列 int 类型。

col1  col2  col3  col4
3 2 3 4
2 2 4 3

我正在尝试以有序的形式选择这些值,例如:

colx  colx  colx  colx
2 3 3 4

列名在结果中无关紧要,也欢迎将结果作为一个列(如果以某种方式连接):

colx
2.3.3.4

谢谢

最佳答案

对于单行,可以使用当前结构来完成,但是如果您有多行,则需要一个主键或任何唯一键。

考虑以下问题

mysql> select * from test ;
+------+------+------+------+------+
| col1 | col2 | col3 | col4 | id |
+------+------+------+------+------+
| 3 | 2 | 3 | 4 | 1 |
| 4 | 7 | 1 | 3 | 2 |
+------+------+------+------+------+

现在查询将是

select 
id,
group_concat(x.col order by x.col separator '.') as colx
from (
select id,col1 as col from test
union all
select id,col2 as col from test
union all
select id,col3 as col from test
union all
select id,col4 as col from test
)x group by id

结果看起来像

+------+---------+
| id | colx |
+------+---------+
| 1 | 2.3.3.4 |
| 2 | 1.3.4.7 |
+------+---------+

关于mysql - 对选定行中的列值进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39013111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com