"Pit" in MySQL, string and numeric comparisons

Source: Internet
Author: User
Tags truncated


Official document:https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html

Original:

Comparison operations result in a value of 1 (TRUE), 0 (FALSE), or NULL. These operations work for both numbers and strings. Strings is automatically converted to numbers and numbers to Strings as necessary.

In other words, a string may be converted to a number when compared.

For a string at the beginning of the data, a number is automatically discarded, leaving only the pure digits to be compared.

For those strings that do not have numbers, when compared to the values, only 0 is left to compare with the other values.



Example:

1. for those strings that do not have numbers, when compared to the values, only 0 is left to compare with the other values. :

Root [(none)] >select 0= ' abc ';

+---------+

| 0= ' abc ' |

+---------+

| 1 |

+---------+

1 row in Set, 1 Warning (0.00 sec)


Root [(none)] >show warnings;

+---------+------+-----------------------------------------+

| Level | Code | Message |

+---------+------+-----------------------------------------+

| Warning | 1292 | Truncated incorrect DOUBLE value: ' abc ' |

+---------+------+-----------------------------------------+

1 row in Set (0.00 sec)



2, for the data at the beginning of the string, converted into a number will automatically discard the following letter part, leaving only pure numbers to compare.

Root [(none)] >select 11= ' 010abc ';

+-------------+

| 11= ' 010ABC ' |

+-------------+

| 0 |

+-------------+

1 row in Set, 1 Warning (0.00 sec)


Root [(none)] >show warnings;

+---------+------+--------------------------------------------+

| Level | Code | Message |

+---------+------+--------------------------------------------+

| Warning | 1292 | Truncated incorrect DOUBLE value: ' 010abc ' |

+---------+------+--------------------------------------------+

1 row in Set (0.00 sec)



The official website gives examples more, more interested can go to see.



In fact, the biggest hole in string and numeric values is that it causes queries not to be indexed and directly affects the efficiency of queries.




"Pit" in MySQL, string and numeric comparisons

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.