简体   繁体   English

从计数查询中获取表名

[英]get table name from count query

How do you get table name from count query, I have multiple queries and I need to get table name from the query results, here is my query 您如何从计数查询中获取表名,我有多个查询,我需要从查询结果中获取表名,这是我的查询

$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"];
..
}

The query is working fine as I can get the number of rows, but I am unable to link the table name to the number of rows (result) 查询工作正常,因为我可以获得行数,但是无法将表名链接到行数(结果)

I have tried this, which executes without error, but unable to get the table name still 我试过了,它执行没有错误,但是仍然无法获得表名

select count(tb_id) as num_rows, 'table1' as TableName from table1

You could put an identifier into your query you can refer to later. 您可以在查询中添加一个标识符,以供以后参考。

$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

Just add it in to the select or the column name. 只需将其添加到选择或列名称中即可。

Column name: 列名:

select count(tb_id) as table1_count from table1; select count(tb1_id) as table2_count from table2.

OR sep column: 或sep列:

(select count(tb_id) as num_rows, 'table1' as tablename from table1)
UNION
(select count(tb1_id), 'table2' from table2...)
UNION.... etc

Edited to add: you are returning sep resultsets; 编辑添加:您将返回sep结果集; consider UNION above for a single resultset. 对于单个结果集,请考虑上面的UNION。

您可以尝试mysqli_fetch_field

mysqli_fetch_field($result)->table;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM