Mike Mike - 4 months ago 10x
SQL Question

Equivalency of RLIKE/REGEXP and LIKE - matching whole string vs. arbitrary substring

I have two queries.

SELECT count(AlbumID)
FROM album
WHERE albumname like '%[%]';

Result: 15733

SELECT count(AlbumName)
FROM album
WHERE AlbumName RLIKE '.*\\[.*\\]';

Result: 15740

So as you can see like returns 7 elements less than rlike. I have two questions why is that? Are the statements not the same? And if I am looking for names that contain lets say
Result: Artist - Song [Live]
Result: Artist- Song [Gold CD, Excplicit Lyrics]
what would query would return the correct result?


The exact equivalency of

LIKE '%[%]' 


REGEXP '^.*\\[.*\\]$'

(note that RLIKE is just a mSQL'ish synonym of REGEXP). In short that means that LIKE always matches the whole string, whilst REGEXP and RLIKE may match any substring.

That is why I would assume that there are apparently seven AlbumNames around like



MyName [abc]!

You may try to determine these seven records by

SELECT AlbumName FROM album 
WHERE AlbumName RLIKE '.*\\[.*\\]' AND AlbumName NOT RLIKE '^.*\\[.*\\]$';