我正在尝试学习SQL,目前我正在学习COUNT函数。我想测试从多个表中提取数据,我想返回一个结果集,如:
| tablename | row_count |
| Computers | 2000 |
| Buildings | 37 |
到目前为止,我还没有找到一种方法来获取信息并制作这样的视图。
答案 0 :(得分:5)
使用union和count(*)和(常量)标签:
select 'Computers' as tablename, count(*) as row_count from Computers
union all
select 'Buildings' as tablename, count(*) as row_count from Buildings
请注意,使用union all
(而不仅仅是union
)意味着返回的行将保持在查询中选择的顺序。