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都不同,我们确定排名的唯一方法是基于字母。没有什么能把他们绑在一起