【问题标题】:MySQL order by field in certain orderMySQL 按特定顺序按字段排序
【发布时间】:2014-02-21 10:17:15
【问题描述】:

我正在尝试在 PHP 中运行 SQL 查询并按 1 列排序,但按特定顺序

我试过这个:

$sql3="SELECT * from extension
where client_id = '".$result2["id"]."' 
ORDER BY FIELD(type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre') ";

但它根本没有订购

【问题讨论】:

  • 您要按哪个字段排序
  • 加载 SQLFiddle 以复制错误。
  • Seems to work well with VARCHAR。您的架构中字段 type 的类型是什么?
  • 表格的排序规则是什么?这会影响列值与文字字符串的比较方式。

标签: php mysql sql


【解决方案1】:

试试这个。希望对你有用。

选择 a.col 作为类型,将 (COUNT,0) 合并为 count 来自
(选择 'A' 作为 col union all
选择 'B' 作为 col union all
选择“C”作为 col union all
选择 'D' 作为 col )a
左连接 Table1 T
在 a.col=T.type
按 FIELD(a.col,'A','B','C','D') 排序;

Order by FIELD in MYSQL

SQL DEMO LINK

【讨论】:

    【解决方案2】:

    试试这个

      ORDER BY  case 
                  when type= 'term'         then 0
                  when type=  'queue'       then 1
                  when type=  'ivr'         then 2
                  when type=  'voicecentre' then 3
                  when type=  'conference'  then 4
                  when type=  'callback'    then 5
                  when type=  'intercom'    then 6
                  when type=  'queuecentre' then 7
              else 8
            end ASC
    

    编辑:

    我猜你必须在这里更改引号

     where client_id = '".$result2['id']."' 
    

    LOOK DEMO FOR MY AND YOUR QUERY

    EDIT2:

    但是如果你使用 gotchas sql 那么这个函数 (ORDER BY FIELD) 将无法正确排序。

    查看article here 并修复它。

    【讨论】:

    • 更标准的 SQL,但有什么特别的原因应该比原来的更好?
    • 没有测试哪个更好,但我给了他其他选择。
    • @JoachimIsaksson men fun du bor nära mig :)
    • 这不是按指定顺序排列的。它只是将它们列为正常
    【解决方案3】:

    从分机中选择术语、队列、ivr、语音中心、会议、回叫、对讲、队列中心 其中client_id = '".$result2["id"]."'

    请尝试按您要选择的顺序给出字段名称

    【讨论】:

      【解决方案4】:

      $sql3=mysql("SELECT * FROM extension WHERE client_id = '".$result2["id"]."' ORDER BY (type, 'term', 'queue', 'ivr', 'voicecentre', '会议', '回拨', '对讲', 'queuecentre')");

      【讨论】:

        【解决方案5】:

        尝试并确保变量 type 设置为提供的字段列表中的值之一。

        FIELD(type, 'term', 'queue', 'ivr', 'voicecentre', 'conference', 'callback', 'intercom', 'queuecentre')
        

        【讨论】:

          【解决方案6】:

          最好的方法是使用find_in_set

          ORDER BY FIND_IN_SET(type, 'term,queue,ivr,voicecentre,conference,callback,intercom,queuecentre')
          

          【讨论】:

            【解决方案7】:
            $sql3 = "SELECT * FROM extension
            WHERE client_id = '".$result2["id"]."' 
            ORDER BY ASC`";
            

            这适用于使用您在表中使用的列名对项目进行排序。

            【讨论】:

              【解决方案8】:
              SELECT *
              FROM Extension,Types
              WHERE Extension.typeID = Types.typeID
                  AND client_id = '".$result2["id"]."' 
              ORDER BY Types.order;
              

              表格类型:

              • [typeID] => PK
              • [类型]
              • [订单]

              表格扩展:

              • [extensionID] => PK
              • [类型ID]
              • ...

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2014-08-17
                • 1970-01-01
                • 1970-01-01
                • 2012-11-08
                • 2013-10-16
                • 2018-03-28
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多