Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between LIKE and = in MYSQL?

Tags:

What's the difference between

SELECT foo FROM bar WHERE foobar='$foo' 

AND

SELECT foo FROM bar WHERE foobar LIKE'$foo' 
like image 993
Ali Avatar asked Jun 16 '09 19:06

Ali


People also ask

What is the difference between like and in SQL?

The LIKE Operator in SQL is used to extract records where a particular pattern is present. In a WHERE clause, the LIKE operator is used to look for a certain pattern in a column. In SQL, it has two wildcard characters, such as: Percentage symbol (%): It is a substitution for zero, one, or more characters.

What is the use of like in MySQL?

The MySQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Which is faster like or in SQL?

1 Answer. Using '=' operator is faster than the LIKE operator in comparing strings because '=' operator compares the entire string but the LIKE keyword compares by each character of the string.

What is the difference between equal and like?

= is a comparison operator that operates on numbers and strings. When comparing strings, the comparison operator compares whole strings. LIKE is a string operator that compares character by character.


1 Answers

= 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'] 
like image 63
lavinio Avatar answered Oct 22 '22 09:10

lavinio