如何从具有数千个表的 mysql 数据库中选择 4 个随机表


How do I select 4 random tables from a mysql database that has thousands of tables?

我需要一种快速、有效的方法来随机返回单个 mysql 数据库中 4 个表的名称。这些表没有结构化的命名模式,除了它们都是小写字母和数字。

例:my_database包含下表:

(cat, dog, boat, car, cars, cars35, accorns, accerns, speaker, shell, olympics, politics, fray, ttypo, ... (thousands of tables like this) ... , road)

我需要一种有效的方法来返回一定数量的随机表的名称而不会重叠。

例:

function_random_table(4) would return 4 table names stored to the following variables:
$random_1 = cars
$random_2 = cars35
$random_3 = shell
$random_4 = cat

只需使用INFORMATION_SCHEMA。表:

select table_name
from INFORMATION_SCHEMA.TABLES t
where TABLE_SCHEMA = 'YOUR_DB_NAME'
order by rand()
limit 4

您可以将所有表放在一个数组中。然后生成四个随机索引,并确保它们都是唯一的索引(因此没有重叠)。获取这些索引,并使用数组中这些索引的位置填充四个 $random_n var。