我正在尝试使用通配符进行 ORDER BY FIELD 工作,但没有成功:
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%')
The colleague_position_id
字段有一个由我们的 MIS 系统生成的文本 ID,我希望以 A 开头的位置显示第一个,F 显示第二个,依此类推。
如果您能提供任何帮助,我们将不胜感激。
Thanks!
这应该可以让您最大程度地控制它:
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
.
此外,您还可以通过以下方式订购positions.colleague_position_id
正如我在示例中所做的那样,因此对于许多人来说positions.colleague_position_id
以相同字母开头的它们仍将按顺序排列。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)