如何在一个查询中计算来自两个表的行? [英] How to count rows from two tables in one query?

查看:75
本文介绍了如何在一个查询中计算来自两个表的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果两个表中的列数相等,则可以在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]

这篇关于如何在一个查询中计算来自两个表的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆