如何从mysql查询中排除表?

时间:2011-12-18 18:00:29

标签: php mysql

$sql = "SHOW TABLES FROM database";

我想排除表“用户”,“汽车”和“礼物”等。怎么办呢?

3 个答案:

答案 0 :(得分:7)

您可以直接从information_schema数据库查询表名。假设您的数据库名称位于变量$your_database_name

mysql_select_db("information_schema");
$sql = "SELECT TABLE_NAME 
  FROM TABLE
  WHERE
    TABLE_SCHEMA = '$your_database_name' 
    AND TABLE_NAME NOT IN ('users','cars','gifts')";

答案 1 :(得分:2)

SHOW TABLES FROM database_name WHERE tables_in_database_name NOT IN ('users');

如果你不想要汽车和用户

SHOW TABLES FROM database_name WHERE tables_in_database_name NOT IN ('users', 'cars'); 

答案 2 :(得分:1)

如果您不想显示表格,那么在echo时您可以这样做:

while($row = mysql_fetch_array($res))
{
   if($row[0] == 'users' || $row[0] == 'cars' || $row[0] == 'gifts')
      continue;
   else
      echo $row[0];
}