列出所有MySQL表并排除特定的表

时间:2016-03-16 11:15:13

标签: php mysql


我尝试从数据库返回所有MySQL表,并排除例如用户的特定表。我使用以下代码打印我的表名:

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>";
    }
}

3 个答案:

答案 0 :(得分:6)

解决方案将是:

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)

以下是一些演示:

from pathlib import Path
for subdir in sorted(Path('/some/path').iterdir()):
    print(subdir)

答案 1 :(得分:2)

如果上述答案无效,请尝试以下方法:

  import sys
    import requests
    from bs4 import BeautifulSoup

    r = requests.get('http://www.hoopsstats.com/basketball/fantasy/nba/opponentstats/16/12/eff/1-1')


    soup = BeautifulSoup(r.text, "html.parser")

    stats = soup.find_all('table', 'statscontent')


    for table in soup.find_all('table', 'statscontent','a'):
        stats = [ stat.text for stat in table.find_all('center') ]
        team = [team.text for team in table.find_all('a')]


        print(team,stats)

答案 2 :(得分:0)

您也可以通过创建一个包含您不想要的表的表来做到这一点: show tables where tables_in_yourDBName not in(select * from disallowed_tables);


disallowed_tables 中列的值是普通 varchar。