在数字前对字符排序

问题描述:

我的问题是,MySQL在数字之前对字符进行排序.举个简单的例子,我们在数据库中有以下字符串:

My problem is, that MySQL orders characters before numbers. To give a quick example, we have this strings in a database:

3, a, 2, 1, b

然后ORDER BY语句将返回以下内容:

And ORDER BY statement would return this:

a, b, 1, 2, 3

但我希望它是

1, 2, 3, a, b

我的数据库是utf8_unicode_ci,但这无济于事.它仍然会产生错误的结果. (我不能只检查它们是否是数字,然后将它们排序到顶部,因为一个项目也可以命名为"2something",并且必须在"asomething"之前).有什么想法吗?

My database is utf8_unicode_ci, but that doesn't help. It still produces the wrong result. (I can't just check if they are numbers and then sort them to the top because an item could also be named "2something" and has to be before "asomething"). Any ideas?

我发现了问题.在数据库(存储用户)中,我按CONCAT(name, login)排序,名称为NULL. MySQL始终将NULL放在顶部,因此字符在数字的顶部(它们的名称字段是一个空字符串).

I found the problem. In the database (which stores users), I was ordering by CONCAT(name, login), and the name was NULL. MySQL orders NULL always at the top, so the characters were top of the numbers (the name field of them was an empty string).

ORDER BY应该以所需的方式工作.

ORDER BY should work in the desired manner if the column type is character or varchar.