Difference between LIKE and = in MYSQL?

后端 未结 12 1883

What\'s the difference between

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

AND

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


        
相关标签:
12条回答
  • 2020-11-29 08:53

    = in SQL does exact matching.

    LIKE does wildcard matching, using '%' as the multi-character match symbol and '_' as the single-character match symbol. '\' is the default escape character.

    foobar = '$foo' and foobar LIKE '$foo' will behave the same, because neither string contains a wildcard.

    foobar LIKE '%foo' will match anything ending in 'foo'.

    LIKE also has an ESCAPE clause so you can set an escape character. This will let you match literal '%' or '_' within the string. You can also do NOT LIKE.

    The MySQL site has documentation on the LIKE operator. The syntax is

    expression [NOT] LIKE pattern [ESCAPE 'escape']
    
    0 讨论(0)
  • 2020-11-29 08:53

    According to the MYSQL Reference page, trailing spaces are significant in LIKE but not =, and you can use wildcards, % for any characters, and _ for exactly one character.

    0 讨论(0)
  • 2020-11-29 08:53

    I think in term of speed = is faster than LIKE. As stated, = does an exact match and LIKE can use a wildcard if needed.

    I always use = sign whenever I know the values of something. For example

    select * from state where state='PA'
    

    Then for likes I use things like:

    select * from person where first_name like 'blah%' and last_name like 'blah%'
    

    If you use Oracle Developers Tool, you can test it with Explain to determine the impact on the database.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-29 09:02

    Looks very much like taken out from a PHP script. The intention was to pattern-match the contents of variable $foo against the foo database field, but I bet it was supposed to be written in double quotes, so the contents of $foo would be fed into the query.

    As you put it, there is NO difference.

    It could potentially be slower but I bet MySQL realises there are no wildcard characters in the search string, so it will not do LIKE patter-matching after all, so really, no difference.

    0 讨论(0)
  • 2020-11-29 09:05

    A little bit og google doesn't hurt...

    A WHERE clause with equal sign (=) works fine if we want to do an exact match. But there may be a requirement where we want to filter out all the results where 'foobar' should contain "foo". This can be handled using SQL LIKE clause alongwith WHERE clause.

    If SQL LIKE clause is used along with % characters then it will work like a wildcard.

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

    Without a % character LIKE clause is very similar to equal sign alongwith WHERE clause.

    0 讨论(0)
提交回复
热议问题