Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql count records in from multiple tables using single query

Tags:

sql

oracle

I have a list of tables for example:

mytableA
mytableB
mytableC

The tables all have same column (timestamp).

I can do a count on each table individually:

select count(*) from mytableA where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';
select count(*) from mytableB where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';
select count(*) from mytableC where timestamp = to_char(sysdate-1, 'yyyymmdd') || '0000';

How can I combine this in one single query? Is there an easy way?

Expected Results:

MyTableName     MyCnt
-----------     -----
mytableA        121
mytableB        78
mytableC        2345
like image 300
cjd143SD Avatar asked Dec 12 '22 03:12

cjd143SD


1 Answers

SELECT  (
    SELECT COUNT(*)
    FROM   table1
    ) AS tot1,
    (
    SELECT COUNT(*)
    FROM   table2
    ) AS tottab2,
    (
    SELECT COUNT(*)
    FROM   table3
    ) AS tottab3
like image 52
COLD TOLD Avatar answered Feb 20 '23 02:02

COLD TOLD