如何从具有数千个表的MySQL数据库中选择四个随机表?
要选择四个随机表,请使用ORDERBYRAND()
。以下是语法-
select TABLE_NAME AS anyAliasName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = ‘yourDatabaseName’; order by rand()limit yourLimitNumber;
让我们实现上述语法,以便从具有数千个表的MySQL数据库中选择四个随机表。
在这里,LIMIT用于设置要获取的记录数。由于我们需要4条记录,因此我们将使用LIMIT4。以下是查询-
mysql> select TABLE_NAME AS Random4TableName from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = 'web' order by rand() limit 4;
这将产生以下输出-
+------------------+ | Random4TableName | +------------------+ | demotable474 | | demotable313 | | demotable452 | | demotable382 | +------------------+ 4 rows in set (0.20 sec)