Sqlite 显示表格,包含有关数据库的信息

问题描述 投票:0回答:2

他们要求我编写一个显示下表的查询:

  • 选择每个表作为字符串
  • 选择属性数量为整数(统计每个表的属性数量)。
  • 使用 COUNT(*) 函数选择行数
  • 使用复合运算符 UNION ALL 将这些行绑定在一起。

我该怎么做?

我知道为了获取每个表的名称,我必须使用

SELECT name AS table_name
  FROM sqlite_schema
 WHERE type = 'table'

但其余的对我来说是个谜。 你愿意帮助我吗?

谢谢!!!

在这里您可以找到数据库

database sqlite create-table
2个回答
0
投票

刚刚有同样的问题,答案归结为“手动”(查看模式可视化或上述输入查询)。 >_<

SELECT 'Customers' AS table_name, 
       13 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Customers
  
UNION ALL

SELECT 'Products' AS table_name, 
       9 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Products

UNION ALL

SELECT 'ProductLines' AS table_name, 
       4 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM ProductLines

UNION ALL

SELECT 'Orders' AS table_name, 
       7 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Orders

UNION ALL

SELECT 'OrderDetails' AS table_name, 
       5 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM OrderDetails

UNION ALL

SELECT 'Payments' AS table_name, 
       4 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Payments

UNION ALL

SELECT 'Employees' AS table_name, 
       8 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Employees

UNION ALL

SELECT 'Offices' AS table_name, 
       9 AS number_of_attribute,
       COUNT(*) AS number_of_row
  FROM Offices;

0
投票

一旦我们从给定表中添加或删除列,可能会导致错误答案,而不是定义固定值,而是使用以下内容:

SELECT "Customer" AS table_name,
       (SELECT count(*) 
          FROM pragma_table_info('customers')) AS number_of_attributes, 
       count(*) AS number_of_rows
  FROM customers;
© www.soinside.com 2019 - 2024. All rights reserved.