按最后3个字符排序 [英] Order by last 3 chars

查看:63
本文介绍了按最后3个字符排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,像这样:

I have a table like:

id  name
--------
1   clark_009
2   clark_012
3   johny_002
4   johny_010

我需要按以下顺序获取结果:

I need to get results in this order:

johny_002
clark_009
johny_010
clark_012

不要问我我已经尝试过什么,我也不知道该怎么做.

Do not ask me what I already tried, I have no idea how to do this.

推荐答案

做到这一点,只需选择最右边的3个字符并按该值升序进行排序即可.

This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending.

SELECT *
FROM table_name
ORDER BY RIGHT(name, 3) ASC;

应该补充的是,随着数据的增长,这将成为一种低效的解决方案.最终,您可能希望将数字附录存储在单独的索引整数列中,以使排序效率达到最佳.

It should be added that as your data grows, this will become an inefficient solution. Eventually, you'll probably want to store the numeric appendix in a separate, indexed integer column, so that sorting will be optimally efficient.

这篇关于按最后3个字符排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆