Jump to content
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`;

 

Share this post


Link to post
Share on other 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

...

Share this post


Link to post
Share on other 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;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.