如何在一个查询中计算来自两个表的行?
问题描述:
我知道,如果两个表中的列数相等,则可以在select语句上使用UNION
.另外,另一个选择是在select子句中使用子查询.我还能使用什么?
I know that one can use UNION
on a select statement if count of columns in two tables is equal. Also, another option is using sub-query in the select clause. What else can I use?
示例:
tabel1 table2
id 1 1
2 2
3 3
我需要在一个查询中从两个表中获取总行数:
I need to get the number of total rows from both tables in one query:
...COUNT(table1.id) as tbc1, COUNT(table2.id) as tbc2...
答
使用子查询,如果需要,请添加FROM DUAL
:
Use sub-queries and if it needs add FROM DUAL
:
SELECT
(SELECT COUNT(*) FROM TABLE1) As Table1Count,
(SELECT COUNT(*) FROM TABLE2) As Table2Count
[FROM DUAL]