mysql 有返回带有序数后缀的数字的函数吗?
基本上我正在寻找类似的东西
SELECT ordinal(my_number) FROM my_table
会返回,
1st
11th
1071st
...
etc
但最好不使用存储过程
Basically I'm looking for something like
SELECT ordinal(my_number) FROM my_table
which would return
1st
11th
1071st
...
etc
but preferrably without the use of a stored procedure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道内置函数,但它很容易编写:
I don't know of a built-in function but it's pretty easy to write:
mysql 不支持这个。无论您从哪种语言获取 mysql 数据,您都必须处理这些字符串。
mysql doesn't have support for this. You'll have to handle the strings in whichever language you are getting the mysql data from.
根据 Ken 的代码,自定义 MySQL 函数如下:
那么它可以用作:
Based on Ken's code, a custom MySQL function would be as follows:
Then it can be used as:
在 MySQL 中可以使用字符串函数,但它很快就会变得混乱。您最好只用您所使用的语言添加后缀。例如,在 PHP 中你可以这样做:
It is possible in MySQL using the string functions but it gets messy real fast. You'd better just do the suffix in the language you're using. For example, in PHP you could do something like this:
我找到了一种对我有用的方法,但它有点像黑客。之所以
有效,是因为目前我正在查看的数字永远不会超过 25。但它不能很好地概括,所以有人可能会对此感到高兴:
但这很多工作量只是为了在 Ken 的代码 更简单。
I found a way that works for me but its a bit of a hack
That works because currently the number I'm looking at never gets above 25. But it doesn't generalize well so someone might be entertained by this:
But that's a lot of work just to get at the
DATE_FORMAT
functionality when Ken's code is simpler.