Home > Database > Mysql Tutorial > MySQL Analysis on the Sorting of Numbers in Strings_MySQL

MySQL Analysis on the Sorting of Numbers in Strings_MySQL

WBOY
Release: 2016-07-06 13:32:45
Original
1272 people have browsed it

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;

Copy after login

or

select * from table where 1 order by id+0 desc;

Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template