当前位置:网站首页>MySQL string sorted by numeric value
MySQL string sorted by numeric value
2022-07-23 16:08:00 【JustDI-CM】
List of articles
1. demand
MySQL When designing a table field , Set up in order to varchar type , It is a value that is accessed , Subsequent businesses need to be sorted according to this field , Get the maximum .
According to the original sorting statement, it is :
SELECT * FROM test_21 a ORDER BY a.testId desc;
The result is :
This is not the desired result .
2. Method
modified
1) And int Calculate
SELECT * FROM test_21 a ORDER BY a.testId+0 desc;
SELECT * FROM test_21 a ORDER BY a.testId-2 desc;
SELECT * FROM test_21 a ORDER BY a.testId*1 desc;
SELECT * FROM test_21 a ORDER BY a.testId/4 desc;
MySQL Of varchar And int When calculating , Will be converted to int.
above 4 individual sql The results of the implementation are 
2)cast() or convert()
SELECT * FROM test_21 a ORDER BY cast(a.testId as SIGNED) desc;
SELECT * FROM test_21 a ORDER BY convert(a.testId,SIGNED) desc;
These two functions are used for format conversion , There's no difference between the two ,
The grammar is :
CAST(expr AS type)
CONVERT(expr,type)
Supported types are
| value | explain |
|---|---|
| DATE | take expr convert to ’YYYY-MM-DD’ Format |
| DATETIME | take expr convert to ’YYYY-MM-DD HH:MM:SS’ Format |
| TIME | take expr convert to ’HH:MM:SS’ Format |
| CHAR | take expr convert to CHAR( Fixed length string ) Format |
| SIGNED | take expr convert to INT( Signed integers ) Format |
| UNSIGNED | take expr convert to INT( An unsigned integer ) Format |
| DECIMAL | take expr convert to FLOAT( Floating point numbers ) Format |
| BINARY | take expr Convert to binary format |
The result is :
边栏推荐
猜你喜欢

Comparison of functional characteristics and parameters of several solar panel battery charging management ICs cs5363, cs5350 and cs5328

One minute rule for sequential disk access

Safe and reasonable use of electricity to harvest a cool "summer"

2022 the most NB JVM foundation to tuning notes, thoroughly understand Alibaba P6 small case

【云原生】持续集成和部署(Jenkins)

Software testing weekly (No. 81): what can resist negativity is not positivity, but concentration; What can resist anxiety is not comfort, but concrete.

【攻防世界WEB】难度三星9分入门题(上):simple_js、mfw

Exclusive interview | open source Summer Star Niu Xuewei

2022最NB的JVM基础到调优笔记,吃透阿里P6小case
![[attack and defense world web] difficulty Samsung 9 points introductory question (Part 1): simple_ js、mfw](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[attack and defense world web] difficulty Samsung 9 points introductory question (Part 1): simple_ js、mfw
随机推荐
Vim到底可以配置得多漂亮?
C语言学习笔记
SSB signal modulation and demodulation based on MATLAB (with source code)
不想dto套dto可以这样写
SharedPreferences data storage
List merging (summer vacation daily question 3)
忘记oracle密码,如何解决
Unity notes ilruntime access
Class homework (5) -- 576. Hungry cattle
Pydensecrf installation
redis 主从复制
A quietly rising domestic software is too strong!
Cookie和Session的区别
冒泡排序-看着一篇就够啦
LeetCode高频题:最少经过几次操作可以使数组变为非降序状态
redis 哨兵模式
Google Earth Engine——影像统计过程中出现的空值问题
Suffix expression (summer vacation daily question 4)
上课作业(5)——#576. 饥饿的牛(hunger)
Mysql—主从复制