Nik
Nik

Reputation: 2484

MySQL Search Within Database (Table Search)

Alright, I'm trying to write a query to display all the tables that contain a certain prefix. Something like what is displayed below (but is obviously incorrect)

SELECT TABLES LIKE chat_

So any table that has the chat prefix, would be displayed. I plan on formatting the output, so it's not going to be a raw output, and I also understand that "what idiot would display table names publicly", and security measures are being taken to prevent that "accidental" table drop (just trying to avoid a flame war). So, how is this accomplished?

Upvotes: 1

Views: 1231

Answers (3)

crazier wu
crazier wu

Reputation: 21

You need to add "in some_db first" before where like below

 SHOW TABLES in test_server_service where 'table' regexp 't_*';

Upvotes: 0

JYelton
JYelton

Reputation: 36512

You can also use regular expressions, which allows a little more flexibility (though a performance cost):

SHOW TABLES WHERE tables_in_db REGEXP 'chat.*';

In this example, replace db with the database name of concern.

Upvotes: 3

Unknown
Unknown

Reputation: 5772

SHOW TABLES LIKE 'chat_%';

Upvotes: 0

Related Questions