Equals(=) vs. LIKE

后端 未结 15 2233
情书的邮戳
情书的邮戳 2020-11-22 15:19

When using SQL, are there any benefits of using = in a WHERE clause instead of LIKE?

Without any special operators, LIKE

15条回答
  •  攒了一身酷
    2020-11-22 15:26

    The equals (=) operator is a "comparison operator compares two values for equality." In other words, in an SQL statement, it won't return true unless both sides of the equation are equal. For example:

    SELECT * FROM Store WHERE Quantity = 200;
    

    The LIKE operator "implements a pattern match comparison" that attempts to match "a string value against a pattern string containing wild-card characters." For example:

    SELECT * FROM Employees WHERE Name LIKE 'Chris%';
    

    LIKE is generally used only with strings and equals (I believe) is faster. The equals operator treats wild-card characters as literal characters. The difference in results returned are as follows:

    SELECT * FROM Employees WHERE Name = 'Chris';
    

    And

    SELECT * FROM Employees WHERE Name LIKE 'Chris';
    

    Would return the same result, though using LIKE would generally take longer as its a pattern match. However,

    SELECT * FROM Employees WHERE Name = 'Chris%';
    

    And

    SELECT * FROM Employees WHERE Name LIKE 'Chris%';
    

    Would return different results, where using "=" results in only results with "Chris%" being returned and the LIKE operator will return anything starting with "Chris".

    Hope that helps. Some good info can be found here.

提交回复
热议问题