【问题标题】:MySQL - How to sort within a record?MySQL - 如何在记录中排序?
【发布时间】:2015-08-21 23:24:48
【问题描述】:

所以我有一个记录行;它是这样的:

data1 data2 data3 data4
4     3     1     2

现在我想按 1 2 3 4 的顺序对数据进行排序。 所以:第 1 行的 data3、data4、data2、data1

有人知道如何以这种方式在记录中排序吗?

【问题讨论】:

  • 您不能这样做,但是您可以使用 unpivot 方法 stackoverflow.com/questions/15184381/… 转换表格,然后对结果进行排序。
  • 我感觉到您没有使用关系表结构的可能性很高......
  • 建议: 1) 不要在 SQL 中这样做;而是在客户端上执行此操作。 2)重新考虑表设计...也许存储在同一行的不同列中的值会更好地存储为单独表中的行。

标签: mysql sorting row record


【解决方案1】:

一旦你有了data1、data2等列,你就需要改变你的数据库模型。大多数情况下,这意味着您需要一个新的额外表格。

错误:

users:
- user_id
- name
- address1
- address2
- address3

右:

users:
- user_id
- name

addresses:
- address_id
- user_id
- address

【讨论】:

    【解决方案2】:

    您可以运行以下代码。 SELECT * FROM table_name ORDER BY coloumn_name ASC;

    您也可以忽略 ASC,因为它意味着按升序排列。

    【讨论】:

    • 不,我不想垂直排序,我想水平排序
    • 好的。然后您可以参考 Gordon Linoff 问题的答案。
    【解决方案3】:

    这里有两种方法。这里是 unpivot 和 repivot 方法:

    select substring_index(group_concat(col order by col), ',', 1) as data1,
           substring_index(substring_index(group_concat(col order by col), ',', 2), ',', -1) as data2,
           substring_index(substring_index(group_concat(col order by col), ',', 3), ',', -1) as data3,
           substring_index(substring_index(group_concat(col order by col), ',', 4), ',', -1) as data4       
    from ((select data1 as col from table) union all
          (select data2 as col from table) union all
          (select data3 as col from table) union all
          (select data4 as col from table)
         ) t
    group by col1, col2, col3, col4;  # A real id would be better for this
    

    【讨论】:

      【解决方案4】:

      一种选择是创建一个简单的函数:

      DELIMITER $$
      
      CREATE FUNCTION get_nth(vn INT, v1 INT, v2 INT, v3 INT, v4 INT)
      RETURNS INT
      BEGIN
          DECLARE v_offset INT;
          DECLARE v_return INT;
          SET v_offset = vn-1; 
          SELECT o.val INTO v_return
            FROM ( SELECT v1 AS val
                    UNION ALL SELECT v2 
                    UNION ALL SELECT v3 
                    UNION ALL SELECT v4 
                    ORDER BY 1
                    LIMIT v_offset,1
                  ) o;
         RETURN v_return;
      END$$
      
      DELIMITER ;
      

      创建该函数后,您可以执行以下操作:

      SELECT get_nth(1,t.data1,t.data2,t.data3,t.data4) AS od1
           , get_nth(2,t.data1,t.data2,t.data3,t.data4) AS od2
           , get_nth(3,t.data1,t.data2,t.data3,t.data4) AS od3
           , get_nth(4,t.data1,t.data2,t.data3,t.data4) AS od4
        FROM ( 
               SELECT 4 AS data1
                    , 3 AS data2
                    , 1 AS data3
                    , 2 AS data4
             ) t
      

      (只需将内联视图 t 替换为对包含列 data1..data4 的表的引用)

      SQL Fiddle demonstration: http://sqlfiddle.com/#!9/e0e39/2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-22
        • 1970-01-01
        • 2017-12-17
        • 1970-01-01
        • 2014-09-26
        • 1970-01-01
        • 2013-07-13
        • 1970-01-01
        相关资源
        最近更新 更多