Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all MySQL tables and exclude specific one

Tags:

php

mysql


I try to return all MySQL tables from Database and exclude specific one for example with users. I use following code to print my table names:

function findTables() {
    global $conn;
    global $DB;

    $query = "SHOW TABLES FROM $DB ";
    $showTablesFromDb = mysqli_query($conn, $query);
    while($row = mysqli_fetch_row($showTablesFromDb)) {
        echo "<li><a href='admin.php?show={$row[0]}'>{$row[0]}</a></li>";
    }
}
like image 478
Ganga Avatar asked Mar 16 '16 11:03

Ganga


People also ask

How do I exclude something in MySQL?

To check records which are NULL, use IS NULL. However, to exclude any of the records, use the NOT IN clause. Use both of them in the same query.

How do I show all columns except one in MySQL?

A MySQL SELECT statement is used to retrieve data from a MySQL table. To exclude certain column(s) from the SELECT statement result, you can omit the column/ field name from the query.

How do I list all the tables in a MySQL database?

MySQL SHOW TABLES command example To use the SHOW TABLES command, you need to log on to the MySQL server first. On opening the MySQL Command Line Client, enter your password. Select the specific database. Run the SHOW TABLES command to see all the tables in the database that has been selected.


1 Answers

The solution will be :

show tables where tables_in_$DB not like 'a%';

Here are some demos:

mysql> show tables;
+-----------------+
| Tables_in_test3 |
+-----------------+
| a1              |
| t1              |
| t2              |
+-----------------+
3 rows in set (0.00 sec)

-- LIKE is simpler than NOT LIKE
mysql> show tables like 'a%';
+----------------------+
| Tables_in_test3 (a%) |
+----------------------+
| a1                   |
+----------------------+
1 row in set (0.00 sec)

-- `show tables not like 'a%'` is not working, 
-- use the following way for NOT LIKE matching
mysql> show tables where tables_in_test3 not like 'a%';
+-----------------+
| Tables_in_test3 |
+-----------------+
| t1              |
| t2              |
+-----------------+
2 rows in set (0.01 sec)
like image 178
Dylan Su Avatar answered Oct 13 '22 01:10

Dylan Su