The difference between the execution result of the mysql statement% and =

SELECT count (1) FROM media_info where media_id ="%"+ "bb"+"%"

SELECT count (1) FROM media_info where media_id like CONCAT ("%", "bb","%")

I suddenly found this problem when I was doing statistics today. About% splicing.

the number of pieces of data returned by the database is not consistent. And also surprised to find that some of the results returned by the first statement do not have the bb keyword. I would like to ask how this difference arises. Generally speaking, when we conduct fuzzy queries, we use that way, thank you.

Mar.21,2021

first of all, ('%'+ 'bb'+'%') doesn't make sense, and the result is 0. When it is compared to a string, the string media_id is forced to be converted to an integer. Unless the string is' 1percent, the converted 1 is not equal to 0, otherwise media_id is always converted to 0, so you can't use it this way.
you can try it specifically:

SELECT 
'' = ('%'+'g'+'%'),
'a' = ('%'+'g'+'%'),
'abc' = ('%'+'g'+'%'),
'1' = ('%'+'g'+'%')
;

to make a fuzzy query, you can only use the second way of your column.


later, I did the test according to the first floor. First, I executed the following two statements
SELECT * FROM media_info WHERE media_id = 0

.

SELECT * FROM media_info WHERE media_id ='%'+ 'bb'+'%'

the result is the same, and the performance confirms that @ Fan Jieqi Jackie is correct, in which case the media is forced to an integer of 0, and the value of [%'+ 'bb'+'%'] is zero. Then I did a test [SELECT * FROM media_info WHERE media_id = 1], and found that the match was the data that media_id starts with 1, so the guess should be, [when using = to make an integer judgment, the first letter will match the corresponding data according to the value you match]

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e486c0-59d00.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e486c0-59d00.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?