如何从计数查询中获取表名,我有多个查询,我需要从查询结果中获取表名,这是我的查询
$myquery = "select count(tb_id) as num_rows from table1; select count(tb1_id) from table2...";
if (myqli_multi_query($connection, $myquery){
..
$row = mysqli_fetch_assoc($result);
$num_rows = $row["num_rows"];
..
}
查询工作正常,因为我可以获取行数,但我无法将表名链接到行数(结果)
我试过这个,执行时没有错误,但无法获取表名
select count(tb_id) as num_rows, 'table1' as TableName from table1
答案 0 :(得分:1)
您可以在查询中添加一个标识符,稍后可以参考。
$myquery = "select 'table1' as tablename, count(tb_id) as num_rows from table1;
select 'table2' as tablename, count(tb1_id) as num_rows from table2;"
// and so on
答案 1 :(得分:0)
只需将其添加到选择列或列名称中。
列名:
select count(tb_id) as table1_count from table1; select count(tb1_id) as table2_count from table2.
OR sep column:
(select count(tb_id) as num_rows, 'table1' as tablename from table1)
UNION
(select count(tb1_id), 'table2' from table2...)
UNION.... etc
编辑添加:你正在返回sep结果集;将UNION视为单个结果集。
答案 2 :(得分:0)
您可以尝试mysqli_fetch_field
mysqli_fetch_field($result)->table;