我有多个表具有相同的列和不同的数据,我正在尝试进行单个查询以从所有这些表中提取数据并将它们视为单个查询。我还需要知道具体结果来自哪个表。 I found this 帮助我从多个表中获取查询,但是当我尝试
时$tables=array("loads", "loads_completed");
foreach ($tables as $table) {
$table = '`'.mb_ereg_replace('`','``',$table).'`';
$sql[] = "(
SELECT *
FROM $table
ORDER BY status
LIMIT 50
)";
}
$sql = implode(' UNION ALL ', $sql);
即使两个表中都有内容,我也会得到Query was empty
。
This是我在显示结果表时发现的,但我不知道如何用表数组形成查询。
答案 0 :(得分:0)
你可以这样做
foreach ($tables as $table) {
$table = '`'.mb_ereg_replace('`','``',$table).'`';
$sql[] = "(
SELECT *,'".$table."' AS tableName
FROM $table
ORDER BY status
LIMIT 50
)";
}