Home >Database >Mysql Tutorial >Introduction to the method of converting varchar type to int type in Mysql database

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

怪我咯
怪我咯Original
2017-03-30 10:57:382728browse

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 two type 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]

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

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

PS:
mysql varchar type conversion int type

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

or

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


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!

Statement:
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