What's the difference between “LIKE” and “=” in SQL?

前端 未结 13 895
悲哀的现实
悲哀的现实 2020-12-05 12:56

Is there any difference between:

SELECT * FROM users WHERE username=\"davyjones\"

and

SELECT * FROM users WHERE username LI         


        
相关标签:
13条回答
  • 2020-12-05 13:35

    LIKE supports wildcards. Usually it uses the % or _ character for the wildcard.

    Using the LIKE operator with no wildcards is the same as using the = operator.

    0 讨论(0)
  • 2020-12-05 13:36

    As far as I know, there is no difference but a time cost to the two selects you wrote. Usually one uses LIKE together with %, meaning 'any string'. I think there's also a character that can be used with LIKE for 'any character', not sure what that is without googling.

    But as your two selects go, the only difference I see is a different run time, since LIKE is used in a regexp-sort-of-fashion.

    0 讨论(0)
  • 2020-12-05 13:40

    Like gets you to work with wild card operators, you may use it in your case for like 'davyjon%' to get all the results starting with davyjon, and to get the exact you may place 'davyjones' and you may also use = in this case

    0 讨论(0)
  • As per SQL standard, the difference is treatment of trailing whitespace in CHAR columns. Example:

    create table t1 ( c10 char(10) );
    insert into t1 values ('davyjones');
    
    select * from t1 where c10 = 'davyjones';
    -- yields 1 row
    
    select * from t1 where c10 like 'davyjones';
    -- yields 0 rows
    

    Of course, assuming you run this on a standard-compliant DBMS. BTW, this is one the main differences between CHARs and VARCHARs.

    0 讨论(0)
  • 2020-12-05 13:44

    LIKE allows partial matching / use of wildcards, while = checks for exact matches.

    For example

    SELECT * FROM test WHERE field LIKE '%oom';
    

    Will return rows where field value is any of the following:

    Zoom, Boom, Loom, Groom
    
    0 讨论(0)
  • 2020-12-05 13:46

    The LIKE condition allows you to use wildcards:

    SELECT * FROM suppliers
    WHERE supplier_name like 'Hew%';
    

    See more examples.

    and Equals = is used for equality matching.

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