Create a test table-- ---------------------------- -- Table structure for check_test -- ---------------------------- DROP TABLE IF EXISTS `check_test`; CREATE TABLE `check_test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `current_price` varchar(10) NOT NULL, `price` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of check_test -- ---------------------------- INSERT INTO `check_test` VALUES ('1', '12', '14'); INSERT INTO `check_test` VALUES ('2', '22', '33'); INSERT INTO `check_test` VALUES ('3', '15', '8'); INSERT INTO `check_test` VALUES ('4', '9', '7'); INSERT INTO `check_test` VALUES ('5', '12', '12.9'); We execute the following SQL: -- Find the records where current_price is smaller than price select * from check_test where current_price<price; select * from check_test where (current_price+0)<(price+0); -- Find the records where current_price is greater than price select * from check_test where current_price>price; select * from check_test where (current_price+0)>(price+0); The results are as follows: We can see that in result 1 , 15>8 does not meet the condition but is found, while in result 3, 15>8 meets the condition but is not found. Why is this? Because the comparison of string type numbers starts from the first digit and compares the corresponding value in the character code. If they are equal, continue to compare the second digit. If they are not equal, the larger value is compared . The ASCII code values corresponding to the numeric characters are as follows: For example:
We can verify this through code, using JavaScript: var current_price = "12"; var price = "14"; console.log(current_price < price); // true var current_price = "15"; var price = "8"; console.log(current_price < price); // true Therefore, numbers that may need to be compared should not be set to varchar type. However, if they have been set to varchar type, then if you want to compare them, convert them to numeric type for comparison and add 0 to the field in SQL. If it is a programming language, it needs to be converted through the corresponding function, and cannot be converted directly by adding 0, such as JavaScript: var current_price = "12"; var price = "14"; console.log(parseInt(current_price) < parseInt(price)); // true var current_price = "15"; var price = "8"; console.log(parseInt(current_price) < parseInt(price)); // false Note: The decimal value corresponding to the character is related to its encoding, but the ASCII code is compatible. Reference Links:Comparison of numbers of string type Comparison of VARCHAR type strings Comparing the size of numbers of varchar type This is the end of this article about how to compare the size of varchar numbers in MySQL database. For more information about comparing the size of MySQL varchar numbers, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to try to add sticky effect to your CSS
>>: Detailed explanation of command to view log files in Linux environment
1. Use Docker Compose to configure startup If you...
This tag is not part of HTML3.2 and is only suppo...
Common scenarios for Nginx forwarding socket port...
Table of contents One master and multiple slaves ...
Therefore, we made a selection of 30 combinations ...
1. mysqlbinlog: [ERROR] unknown variable 'def...
We hope to insert the weather forecast into the w...
Detailed analysis of SQL execution steps Let'...
Table of contents 1. MySQL join buffer 2. JoinBuf...
After configuring VIP, the error message that app...
The MySQL built on Tencent Cloud is always very s...
It is mainly the configuration jump of the if jud...
This article shares the installation and configur...
<br />This section introduces how to impleme...
1. Find out whether MySQL was installed before Co...