如何在MYSQL中对列进行排序

时间:2018-07-24 09:31:59

标签: mysql sql sorting

我每行都有三个字段,我想将它们串联起来,并按升序排列:

select * concat(field1, field2, field3) AS result
from tbl 

我的数据是:

field1, field2, field3
   7       2       9
   4       7       8
   6       3       2 

预期结果是:

field1, field2, field3   result
  7       2       9       279
  4       7       8       478
  6       3       2       236

我该怎么做?

6 个答案:

答案 0 :(得分:3)

Use LEAST ,GREATEST function technically

   SELECT field1, field2, field3,
   concat(LEAST(field1,field2,field3), 
   (field1 + field2 + field3) - (GREATEST(field1,field2,field3)+
   LEAST(field1,field2,field3)),
   GREATEST(field1,field2,field3)
   ) 
   from tbl

答案 1 :(得分:2)

You can try GROUP_CONCAT approach. It can be extended to any number of columns:

CREATE TABLE testdata(id INT PRIMARY KEY, field1 INT, field2 INT, field3 INT);
INSERT INTO testdata (id, field1, field2, field3) VALUES
(1, 7, 2, 9),
(2, 4, 7, 8),
(3, 6, 3, 2);

SELECT testdata.id, field1, field2, field3, GROUP_CONCAT(rtc.fields ORDER BY fields SEPARATOR '') AS fields_sorted
FROM testdata
INNER JOIN (
    SELECT id, field1 AS fields FROM testdata UNION ALL
    SELECT id, field2 AS fields FROM testdata UNION ALL
    SELECT id, field3 AS fields FROM testdata
) AS rtc ON testdata.id = rtc.id
GROUP BY testdata.id, field1, field2, field3;

Output:

+----+--------+--------+--------+---------------+
| id | field1 | field2 | field3 | fields_sorted |
+----+--------+--------+--------+---------------+
|  1 |      7 |      2 |      9 | 279           |
|  2 |      4 |      7 |      8 | 478           |
|  3 |      6 |      3 |      2 | 236           |
+----+--------+--------+--------+---------------+

答案 2 :(得分:1)

请尝试解决方案,尽管我尚未测试过。您只需稍作修改,就能实现您想要的。任何改进,请在评论中提及。

select id, concat(MIN_VAL, MIDDLE_VAL, MAX_VAL) 
from
   ( SELECT ID,
        (CASE
            WHEN field1 >= field2 AND field1 >= field3 THEN field1
            WHEN field2 >= field1 AND field2 >= field3 THEN field2 
            WHEN field3 >= field1 AND field3 >= field2 THEN field3
            ELSE  field1 END) MAX_VAL

    ,Case When field1 < field2 And field1 < field3 Then field1 
                When field2 < field1 And field2 < field3 Then field2 
                Else field3
                End As MIN_VAL
    ,Case When (field1 >= field2 And field1 <= field3) OR  (field1 <= field2 And field1 >= field3) Then field1 
                When (field2 >= field3 And field2 <= field1) OR (field2 <= field3 And field2 >= field1)Then field2
                 WHEN (field3 >= field1 AND field3 <= field2) OR  (field3 <= field1 AND field3 >= field2) THEN field3 
                Else field1
                End As MIDDLE_VAL

       From table
) int_pass

答案 3 :(得分:1)

一种更优雅的解决方案是利用本机json_extract函数根据其键升序提取值。


select json_extract('{"10":10, "5": 5}', '$.*'); // [5, 10]
  1. 使用json_object函数构建json对象,并为键和值使用相同的字段

  2. 使用json_extract

  3. 以所需顺序提取值
  4. [,]替换为空白

select json_extract( json_object(f1, f1, f2, f2, f3, f3), '$.*' )

假设f1 = 5, f2 = 4 and f3 = 1,您将得到[1,4,5]

现在您可以使用替换字符替换不需要的字符

select replace( replace( replace( json_extract( json_object(f1, f1, f2, f2, f3, f3), '$.*' ), '[', ''), ']', ''), ', ', '') from t;

答案 4 :(得分:0)

假设,您要按字段field2,field1,field3的顺序将列作为字符串串联

尝试:

select * ,concat(field2,field1,field3) as "result" from tbl;

enter image description here

答案 5 :(得分:0)

您可以将MySQL的CONCAT功能用作:

SELECT *, CONCAT(field1, field2, field3) AS result from tbl;