Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL按字段排序,带%_Mysql_Sorting_Sql Order By - Fatal编程技术网

MySQL按字段排序,带%

MySQL按字段排序,带%,mysql,sorting,sql-order-by,Mysql,Sorting,Sql Order By,我正在尝试使用通配符通过现场工作下达订单,但未成功: SELECT positions.*, departments.dept_name, departments.dept_url, divisions.dept_name AS div_name FROM positions LEFT JOIN departments ON positions.colleague_dept_code = departments.collea

我正在尝试使用通配符通过现场工作下达订单,但未成功:

SELECT positions.*, 
       departments.dept_name, 
       departments.dept_url, 
       divisions.dept_name AS div_name
FROM positions LEFT JOIN departments 
           ON positions.colleague_dept_code = departments.colleague_code 
     LEFT JOIN departments AS divisions 
          ON positions.colleague_div_code = divisions.colleague_code
WHERE colleague_id = '$colleague_id'
ORDER BY FIELD(positions.colleague_position_id, 'A%', 'F%', 'T%', 'S%', 'C%')
同事职位id
字段有一个由我们的MIS系统生成的文本id,我希望以a开头的职位先显示,以F开头的职位再显示,等等

如果您能提供任何帮助,我们将不胜感激


谢谢

删除
通配符如何

按字段排序(positions.component\u position\u id'A',F',T',S',C')


这将使您能够最大限度地控制它:

order by
  case left(positions.colleague_position_id, 1)
    when 'A' then 1
    when 'F' then 2
    when 'T' then 3
    when 'S' then 4
    when 'C' then 5
    else 6
  end, positions.colleague_position_id
这是因为您可以将所有不匹配的值发送到您想要的位置(在本例中是在末尾)。对于不匹配的值,
field()
函数将返回
0
,并将它们放在结果集的顶部,甚至在以
A
开头的值之前


此外,您还可以像我在示例中所做的那样,按
职位。同事职位id
排序,这样对于许多
职位。以相同字母开头的同事职位id
仍然可以排序。

您能为您的职位创建一个单独的表,并具有“排名”列吗?然后,您可以通过positiron.rank.No加入现有的查询和订单,因为不幸的是,每个职位ID都不同,我们确定排名的唯一方法是基于字母。没有什么能把他们绑在一起