Difference between LIKE and = in MYSQL?

后端 未结 12 1926

What\'s the difference between

SELECT foo FROM bar WHERE foobar=\'$foo\'

AND

SELECT foo FROM bar WHERE foobar LIKE\'$foo\'         


        
12条回答
  •  旧时难觅i
    2020-11-29 09:02

    Please bear in mind as well that MySQL will do castings dependent upon the situation: LIKE will perform string cast, whereas = will perform int cast. Considering the situation of:

        (int)   (vchar2)
    id  field1  field2
    1   1       1
    2   1       1,2
    

    SELECT * FROM test AS a LEFT JOIN test AS b ON a.field1 LIKE b.field2

    will produce

    id  field1  field2  id  field1  field2
    1   1       1       1   1       1
    2   1       1,2     1   1       1
    

    whereas

    SELECT * FROM test AS a LEFT JOIN test AS b ON a.field1 = b.field2

    will produce

    id  field1  field2  id  field1  field2
    1   1       1       1   1       1
    1   1       1       2   1       1,2
    2   1       1,2     1   1       1
    2   1       1,2     2   1       1,2
    

提交回复
热议问题