我有一张表格,其中我有4列int类型。
col1 col2 col3 col4
3 2 3 4
2 2 4 3
我试图以有序的形式选择这些值,如:
colx colx colx colx
2 3 3 4
列名在结果中无关紧要,也有结果,因为欢迎一个col(如果以某种方式连接):
colx
2.3.3.4
由于
答案 0 :(得分:3)
对于单行,可以使用当前结构完成,但如果您有多行,则需要主键或任何唯一键。
考虑以下
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 |
+------+---------+