Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

ivanferrer

Como contar registros de várias tabelas numa única query

Recommended Posts

Gostaria de saber se existe alguma maneira de retornar o total de registros para cada tabela em uma mesma query...tipo algo como isso mas que funcione:


 

SELECT (SELECT COUNT(*) FROM `tab_banners`) AS TOTAL_BANNER,
 (SELECT COUNT(*) FROM `tab_categorias`) AS TOTAL_CATEGORIA,
 (SELECT COUNT(*) FROM `tab_documentos`) AS TOTAL_DOCUMENTO,
 (SELECT COUNT(*) FROM `tab_galeria`) AS TOTAL_GALERIA,
 (SELECT COUNT(*) FROM `tab_noticias`) AS TOTAL_NOTICIAS,
 (SELECT COUNT(*) FROM `tab_paginas`) AS TOTAL_PAGINAS,
 (SELECT COUNT(*) FROM `tab_produto`) AS TOTAL_PRODUTOS,
 (SELECT COUNT(*) FROM `tab_revendas`) AS TOTAL_REVENDAS,
 (SELECT COUNT(*) FROM `tab_usuario`) AS TOTAL_USUARIOS,
 (SELECT COUNT(IF((`tab_usuario`.`nivel_acesso`=3 OR `tab_usuario`.`nivel_acesso`=4),*,0)) ) AS TOTAL_USUARIOS_ADMINS ) as TOTAL_DOS_TOTAIS

FROM
    `tab_banners`,
    `tab_categorias`,
    `tab_documentos`,
    `tab_galeria`,
    `tab_noticias`,
   `tab_paginas`,
   `tab_produto`,
   `tab_revendas`,
   `tab_usuario`;

 

Compartilhar este post


Link para o post
Compartilhar em outros sites


select 'tabela1',count(*) qtd from tabela1

union all

select 'tabela2',count(*) qtd from tabela2

union all

select 'tabela3',count(*) qtd from tabela3

union all

...

Compartilhar este post


Link para o post
Compartilhar em outros sites

Ola,

use o information_schema

exemplo:

select table_name, TABLE_ROWS
FROM information_schema.TABLES
where table_name in ('tabela1', 'tabela2')
group by table_name;
B)
select table_name, TABLE_ROWS
FROM information_schema.TABLES
where table_name in ('tabela1', 'tabela2')
group by table_name;

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.