1

I'm trying to select the tables names in my database using this query

show tables LIKE 'table1%'

it's working fine but at the same table I have a version of the table with some columns empty called 'table1_blank'. When I use the above statement I get every table starting with table1 including the "blank", how do I exclude 'table1_blank' from the selection?

Thanks.

2 Answers 2

1

You can use multiple like clauses using the where condition for SHOW TABLES:

SHOW TABLES 
FROM `dbname` 
WHERE `Tables_In_dbname` LIKE 'table1%' 
  AND `Tables_In_dbname` NOT LIKE 'table1\_%';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It still returned table1 and table1_blank but I used it with Junsu Heo's other way of selecting tables using information_schema and it worked.
Hmm, I would have had to seen the query that was ran. I created two tables called table1 and table1_blank to test it.
0

'_' has special meaning. 'any one character', so you need to change like this..

EDITED

SELECT TABLE_NAME
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'your database name'
   AND TABLE_NAME LIKE 'table1%'
   AND TABLE_NAME NOT LIKE 'table1\_%';

2 Comments

show tables LIKE 'table1_%' this is giving me table1_blank only.
@crimson oops. I misunderstood you question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.