The example in this article describes the MySQL problem of sorting numbers in strings. Share it with everyone for your reference, the details are as follows:
I believe everyone is familiar with MySQL strings. We often encounter some problems when sorting MySQL strings, such as the one to be introduced below
Today I solved a very strange problem about MySQL string sorting. The varchar type is defined in the data, but the actual data stored is Int type. Click the query statement to sort:
Put field*1 or 0 to sort MySQL string fields numerically
For example:
select * from table where 1 order by id*1 desc;
or
select * from table where 1 order by id+0 desc;
In addition to the above methods, here is a sorting method, using find_in_set() for invincible sorting
Attached is the usage of Mysql function find_in_set():
The code is as follows:
$sql = "select p.*, find_in_set(p.products_id,$string_hot_pid) as rank from products p where p.products_id in ($string_hot_pid) order by rank";
If you have any good ideas and suggestions, please leave your valuable comments so that we can make progress together
Readers who are interested in more MySQL-related content can check out the special topics on this site: "A Complete Collection of MySQL Log Operation Skills", "A Summary of MySQL Transaction Operation Skills", "A Complete Collection of MySQL Stored Procedure Skills", and "A Summary of MySQL Database Lock Related Skills" 》and《Summary of commonly used functions in MySQL》
I hope this article will be helpful to everyone in MySQL database planning.