Wildcards in SQL doesn't return the expected output

Viewed 23

I tried to run a SQL script in MySQL DB as below

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'ACT_%' OR TABLE_NAME LIKE 'IMS_PPH%';

In the response it is showing 'actor' and 'actor_info' table names even though I filtered with 'ACT_%'.

1 Answers

In MySQL, the underscore character ('_') is a wildcard that matches any character, which is why "ACT_" matches tables beginning with "ACT" followed by any character, which includes your "ACTOR" table.

If you want to treat "_" as a literal underscore, you must escape it, e.g. "ACT\_%".

Related