mysql automation type conversion issues

mysql automation type conversion issues

background 

There is a demand for service, used to find_in_set functions, the simple paste, as follows:

SELECT FIND_IN_SET('b','a,b,c,d'); //返回值为2,即第2个值
实际用法:
select * from campaign_plan where find_in_set('4',ad_type);

参考:https://www.cnblogs.com/mytzq/p/7090197.html

Accidentally discovered one such issue, ad _type as text type, specific table data as follows:
image.png
the implementation of the statement is as follows:
image.png
suddenly Three Views collapse, what, how to match the first number back then?

Doubts

Asked a friend, he gave a direction, mysql will automatically convert type when you select an inconsistent statement of condition and data type;
Yipaitaitui, yes, there are encountered similar problems before, as follows:

There is id varchar, you do not add '' can also check out and should be converted, but this did not take the index, so the query efficiency is very low. .

Problem was encountered is that when the data is id = '123', you select is where id = 123, one int is a varchar, will automatically mysql '123' 123 into contrast with your
back, above me problems encountered is similar, but the presence of a comma, the conversion will be truncated when the actual operation is as follows:

image.png

So the interpretation on the three recovery outlook

I took a look at the official translated version,
implicit type conversion rules:

  • If one or both arguments are NULL, the result of the comparison is NULL, NULL in addition to security <=> equal comparison operator. For NULL <=> NULL, the result is true. No conversion
  • If the comparison operation two parameters are strings, they are compared as strings.
  • If both arguments are integers, then they are compared as integers.
  • If it does not compare with the numbers, then the hexadecimal values ​​as binary strings
  • If the parameter is a decimal value, the comparator dependent on another parameter. If another parameter is an integer or a decimal value, then the parameters are compared with the decimal value, if the other parameters are floating point value, the parameter values ​​are compared with the floating point
  • If a parameter is TIMESTAMP or DATETIME column, the other parameters are constant, the comparison constant timestamp before converting in execution.
  • In all other cases, the parameters are used as floating-point (real) comparison.

Practical examples:

mysql> SELECT 1+'1';
        -> 2
mysql> SELECT CONCAT(2,' test');
        -> '2 test'
        
mysql> SELECT 1 > '6x';
        -> 0
mysql> SELECT 7 > '6x';
        -> 1
mysql> SELECT 0 > 'x6';
        -> 0
mysql> SELECT 0 = 'x6';
        -> 1

Reference material

https://zhuanlan.zhihu.com/p/30955365
https://dev.mysql.com/doc/refman/5.5/en/type-conversion.html

Guess you like

Origin www.cnblogs.com/jwentest/p/11694431.html