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

前端 未结 13 894
悲哀的现实
悲哀的现实 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:21

    LIKE allows wildcards like % (any number of characters here) and _ (one character here).

    SELECT * FROM users WHERE username LIKE 'joe%'
    

    Selects all usernames starting with joe.

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

    LIKE searches for a pattern.

    /* Returns all users whose username starts with "d" */
    SELECT * FROM users WHERE username LIKE 'd%'
    
    /* Returns all users whose username contains "dav" */
    SELECT * FROM users WHERE username LIKE '%dav%'
    
    0 讨论(0)
  • 2020-12-05 13:22

    That will give you the same result. However, LIKE allows wildcards, for example...

    SELECT * FROM users WHERE username LIKE 'davy%'
    

    The only syntax problem was double quotes instead of single quotes

    0 讨论(0)
  • 2020-12-05 13:31
    create table A (id int,name varchar(30))
    
    insert into A values(4,'subhash')
    

    Use the trailing whitespace to search the name field:

    select * from A where name='Subhash '
    --Yields 1 row
    select * from A where name like 'Subhash '
    --Yields 0 row
    
    0 讨论(0)
  • 2020-12-05 13:31

    Like is pattern matching operator and = is exact matching operator. i.e. where name like W% it means start with W and after that one or more characters and = i.e. where name ='James' this is exact matching

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

    Equals '=' is just for equality. On the other hand, LIKE supports SQL wildcard matching.

    So, with LIKE you can do name like '%jones' to get all the names ending in jones. With LIKE, the percent '%' character is anything, length zero or more, and the underscore character, '_', is any one character.

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