简体   繁体   中英

php mysql - select table starting with underscore

How do you get a table that starts with an underscore?

This works for getting all tables that start with a capital T:

SHOW TABLES LIKE 'T%'

This doesn't work (returns all tables):

SHOW TABLES LIKE '_%'

As the underscore is a wildcard, you need to escape it to match the underscore itself:

SHOW TABLES LIKE '\_%'

From MySQL.com - 3.3.4.7 Pattern Matching :

SQL pattern matching enables you to use “_” to match any single character and “%” to match an arbitrary number of characters (including zero characters)

The underscore is a wildcard in sql LIKE statements and stand for 1 of any character :)

Use like SHOW TABLES LIKE '___ADMIN' to get anything like SYSADMIN or ORGADMIN but not ADMIN or FOOBARADMIN.

To list all tables that start with an underscore you need to escape it.

SHOW TABLES LIKE '\\_%'

This must work:

mysql > SHOW TABLES LIKE '\_%';

Because as the documentation states:

To test for literal instances of a wildcard character, precede it by the escape character. If you do not specify the ESCAPE character, “\\” is assumed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM