如何用SQL语句查询表名和行数

时间:2021-10-02 21:53:58
我需要做个查询,这个查询会给出表名和行数。这可能吗?

  Rudy Limeback:

  是的,这相当简单。(这里不是很明白。。。)

select 'Customers' as tablename

  , count(*) as row_count

  from Customers

  union all

  select 'Orders' as tablename

  , count(*) as row_count

  from Orders

  union all

  select 'Products' as tablename

  , count(*) as row_count

  from Products

  union all

  ...

  如果你需要在你的数据库里的所有表上做这个查询,那你可以从INFORMATION_SCHEMA.TABLES视图生成表清单: 

select table_name

  from information_schema.tables

  where table_schema = 'mydatabase'

  然后你可以利用这个查询的结果集和一个文本编辑器生成第一个查询。