여러 테이블에서 개수 (*)를 선택하십시오.
결과 count(*)
가있는 두 개의 다른 테이블 (호출 tab1
및 tab2
) 에서 어떻게 선택할 수 있습니까?
Count_1 Count_2
123 456
나는 이것을 시도했다 :
select count(*) Count_1 from schema.tab1 union all select count(*) Count_2 from schema.tab2
그러나 내가 가진 모든 것 :
Count_1
123
456
SELECT (
SELECT COUNT(*)
FROM tab1
) AS count1,
(
SELECT COUNT(*)
FROM tab2
) AS count2
FROM dual
추가 정보로서 SQL Server에서 동일한 작업을 수행하려면 쿼리의 "FROM dual"부분 만 제거하면됩니다.
약간 다른 이유만으로도 :
SELECT 'table_1' AS table_name, COUNT(*) FROM table_1
UNION
SELECT 'table_2' AS table_name, COUNT(*) FROM table_2
UNION
SELECT 'table_3' AS table_name, COUNT(*) FROM table_3
그것은 답변을 바꿨습니다 (한 열 대신 테이블 당 한 행). 그렇지 않으면 크게 다르다고 생각하지 않습니다. 나는 성능면에서 그것들이 동등해야한다고 생각합니다.
내 경험은 SQL Server에 관한 것이지만 다음과 같이 할 수 있습니다.
select (select count(*) from table1) as count1,
(select count(*) from table2) as count2
SQL Server에서는 결과가 나옵니다.
다른 약간 다른 방법 :
with t1_count as (select count(*) c1 from t1),
t2_count as (select count(*) c2 from t2)
select c1,
c2
from t1_count,
t2_count
/
select c1,
c2
from (select count(*) c1 from t1) t1_count,
(select count(*) c2 from t2) t2_count
/
다른 답변을 볼 수 없으므로 이것을 제기하십시오.
경우 당신은 하위 쿼리 싫어 하고 당신이 할 수있는 각 테이블의 기본 키를 가지고 :
select count(distinct tab1.id) as count_t1,
count(distinct tab2.id) as count_t2
from tab1, tab2
그러나 성능면에서는 Quassnoi의 솔루션이 더 좋으며 내가 사용하는 솔루션이라고 생각합니다.
select (select count(*) from tab1) count_1, (select count(*) from tab2) count_2 from dual;
SELECT (SELECT COUNT(*) FROM table1) + (SELECT COUNT(*) FROM table2) FROM dual;
여기에서 나에게 공유
옵션 1-다른 테이블의 동일한 도메인에서 계산
select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain1.table2) "count2"
from domain1.table1, domain1.table2;
옵션 2-동일한 테이블에 대해 다른 도메인에서 계산
select distinct(select count(*) from domain1.table1) "count1", (select count(*) from domain2.table1) "count2"
from domain1.table1, domain2.table1;
옵션 3-행이 여러 개인 "union all"이있는 동일한 테이블에 대해 다른 도메인에서 계산
select 'domain 1'"domain", count(*)
from domain1.table1
union all
select 'domain 2', count(*)
from domain2.table1;
SQL을 즐기십시오, 나는 항상합니다 :)
약간의 완성도를 위해-이 쿼리는 쿼리를 생성하여 지정된 소유자에 대한 모든 테이블 수를 제공합니다.
select
DECODE(rownum, 1, '', ' UNION ALL ') ||
'SELECT ''' || table_name || ''' AS TABLE_NAME, COUNT(*) ' ||
' FROM ' || table_name as query_string
from all_tables
where owner = :owner;
출력은 다음과 같습니다
SELECT 'TAB1' AS TABLE_NAME, COUNT(*) FROM TAB1
UNION ALL SELECT 'TAB2' AS TABLE_NAME, COUNT(*) FROM TAB2
UNION ALL SELECT 'TAB3' AS TABLE_NAME, COUNT(*) FROM TAB3
UNION ALL SELECT 'TAB4' AS TABLE_NAME, COUNT(*) FROM TAB4
그런 다음 카운트를 얻기 위해 실행할 수 있습니다. 때때로 가지고 다니기에 편리한 스크립트입니다.
빠른 찌르기 :
Select (select count(*) from Table1) as Count1, (select count(*) from Table2) as Count2
Note: I tested this in SQL Server, so From Dual
is not necessary (hence the discrepancy).
select
t1.Count_1,t2.Count_2
from
(SELECT count(1) as Count_1 FROM tab1) as t1,
(SELECT count(1) as Count_2 FROM tab2) as t2
If the tables (or at least a key column) are of the same type just make the union first and then count.
select count(*)
from (select tab1key as key from schema.tab1
union all
select tab2key as key from schema.tab2
)
Or take your satement and put another sum() around it.
select sum(amount) from
(
select count(*) amount from schema.tab1 union all select count(*) amount from schema.tab2
)
Declare @all int
SET @all = (select COUNT(*) from tab1) + (select count(*) from tab2)
Print @all
or
SELECT (select COUNT(*) from tab1) + (select count(*) from tab2)
--============= FIRST WAY (Shows as Multiple Row) ===============
SELECT 'tblProducts' [TableName], COUNT(P.Id) [RowCount] FROM tblProducts P
UNION ALL
SELECT 'tblProductSales' [TableName], COUNT(S.Id) [RowCount] FROM tblProductSales S
--============== SECOND WAY (Shows in a Single Row) =============
SELECT
(SELECT COUNT(Id) FROM tblProducts) AS ProductCount,
(SELECT COUNT(Id) FROM tblProductSales) AS SalesCount
JOIN with different tables
SELECT COUNT(*) FROM (
SELECT DISTINCT table_a.ID FROM table_a JOIN table_c ON table_a.ID = table_c.ID );
select (select count() from tab1 where field
like 'value') + (select count() from tab2 where field
like 'value') count
select @count = sum(data) from
(
select count(*) as data from #tempregion
union
select count(*) as data from #tempmetro
union
select count(*) as data from #tempcity
union
select count(*) as data from #tempzips
) a
참고URL : https://stackoverflow.com/questions/606234/select-count-from-multiple-tables
'Programming' 카테고리의 다른 글
Java를 사용하여 과학적 표기법없이 이중 값을 인쇄하려면 어떻게합니까? (0) | 2020.05.07 |
---|---|
R에서 벡터를 청크로 나눕니다. (0) | 2020.05.07 |
비활성화 된 입력 값은 제출되지 않습니다 (0) | 2020.05.07 |
파이썬은 파일을 얼마나 자주 플러시합니까? (0) | 2020.05.07 |
Visual Studio가 Internet Explorer 대신 기본 브라우저를 엽니 다 (0) | 2020.05.07 |