0

I have the below code to display a list of all tables in my database.

$table_count = mysql_query("
    SELECT TABLE_NAME 
    FROM information_schema.tables 
    WHERE table_schema = 'DB_NAME'
"); 
$tables = array();
while($row = mysql_fetch_array($table_count))
{
    $table = $row["TABLE_NAME"];
}

Is it possible to change this query to show all tables EXCEPT some?

If so can someone explain/show me how to do it?

2 Answers 2

2

Sure you can. You can either select some columns

SELECT foo, bar, baz from ...

or you specify where not by chaining your conditions with AND or OR

$table_count = mysql_query("
SELECT TABLE_NAME AS tableName, another_complicated_column as niceName 
FROM information_schema.tables 
WHERE table_schema = 'DB_NAME' 
AND another_column NOT LIKE 'FOOBARBAZ' 
AND another_another_column != 333
"); 
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect answer, thank you. I dont know if I need to ask this as another question but is it possible to show the table names "nicer"? EG instead of client_info show Client Information
Yes, just use as keyword. Not sure if you are allowed spaces though
Well, yes you can do this, though you shouldn't use whitespaces. Whitespaces will make handling the data really hard, and I'm not really sure if you can use them either way. I would go with camelCase or lodash only @user3092953
I've edited it in my answer in the second select block to show you how to give an alias to a column with AS
Thank you, great help :)
1

If you change your query, you could filter out tables by name, for example:

$table_count = mysql_query(
"SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = 'DB_NAME' AND TABLE_NAME NOT LIKE 'TMP_%' "
);

This would filter out tables that begin with TMP_, leaving all others.

Comments

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.