Introduction to the method of converting varchar type to int type in Mysql database

怪我咯
Release: 2017-03-30 10:57:38
Original
2603 people have browsed it

In the last article, I told you about the slow query caused by converting int to varchar in the databaseMySQL. This article will introduce to you the conversion of varchar in the Mysql database. Let’s take a look at the method of converting type to int type!

mysql provides us with twotype conversionfunctions: CAST and CONVERT. How can we let go of ready-made things?

The CAST() and CONVERT() functions can be used to obtain a value of one type and produce a value of another type.

This type can be one of the following values:

BINARY[(N)] CHAR[(N)] DATE DATETIME DECIMAL SIGNED [INTEGER] TIME UNSIGNED [INTEGER]
Copy after login

So we can also use CAST to solve the problem:

select server_id from cardserver where game_id = 1 order by CAST(server_id as SIGNED) desc limit 10
Copy after login

You can also use CONVERT to solve this problem:

select server_id from cardserver where game_id = 1 order by CONVERT(server_id,SIGNED) desc limit 10
Copy after login

PS:
mysql varchar type conversion int type

select * from gyzd_yysinfo order by cast(yysid as SIGNED INTEGER)
Copy after login

or

select * from gyzd_yysinfo order by cast(yysid as UNSIGNED INTEGER)
Copy after login


The above is the detailed content of Introduction to the method of converting varchar type to int type in Mysql database. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!