Forum Moderators: coopster

Message Too Old, No Replies

Maybe a silly question about MySQL

Is "LIKE" same as "="?

         

isorg

9:47 am on Mar 29, 2005 (gmt 0)

10+ Year Member



In my MySQL queries, I tend to use "LIKE" and "=" interchangably as I always assumed they were the same function.

Is this correct, or do they mean different things? :-)

tomda

10:05 am on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



They are different.
Try to use the LIKE statement as less as possible as it is time consuming for large database.

Example
*******
ROBERT
ROBERTA
AROBERT

select name from dbase where name = 'ROBERT' will return 1 result (ROBERT)

select name from dbase where name like 'ROBERT' will return 1 result (ROBERT)

select name from dbase where name like '%ROBERT' will return 1 result (AROBERT)

select name from dbase where name like 'ROBERT%' will return 1 result (ROBERTA)

select name from dbase where name like '%ROBERT%' will return 3 result (AROBERT, ROBERTA, ROBERT)

grandpa

12:21 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe a silly question about MySQL

Nothing silly about the question at all. Thanks tomda for the usage description. I know this is silly but, I guess the first time I saw an example of LIKE the percent sign was used on both ends. So I always use it that way and never considered the possibility of left or right matching. sigh....

isorg

8:19 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



Many thanks to both of you for the helpful replies. :)

EVOrange

8:28 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



Just curious Tomda, but wouldn't,

select name from dbase where name ='%ROBERT%' will return 3 result (AROBERT, ROBERTA, ROBERT) also?

EVO

brendan3eb

8:38 pm on Mar 29, 2005 (gmt 0)

10+ Year Member



I THINK that will check for the literal %ROBERT% with percents and all in table.

coopster

11:51 pm on Mar 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Correct. One correction on Tomda's SELECT statements though. The '%' matches any number of characters, even zero characters. Therefore,

select name from dbase where name like '%ROBERT' will return 2 results (ROBERT, AROBERT)

select name from dbase where name like 'ROBERT%' will return 2 results (ROBERT, ROBERTA)