SQLite:计算多个表中的记录数

时间:2019-12-03 13:55:06

标签: sqlite count record

使用SQLite,我可以在数据库中获取所有表名:

SELECT name AS Tablename FROM sqlite_master WHERE type = 'table'

结果将是一些表名,例如:

表名:
汽车
飞机
巴士

我如何进行SQL查询,该查询将对找到的每个表的记录数进行计数,结果应为:

表名记录:
汽车100
飞机200
公交300

我了解在此示例中,我仅可以运行3条SELECT COUNT()语句,但是表的数量可以变化,因此我无法对固定数量的SELECT COUNT()进行硬编码

1 个答案:

答案 0 :(得分:1)

语句的所有表名和列名在编译时都必须知道,因此您不能动态地做到这一点。

您必须根据从<th:block th:each="spot: ${spots}"> <select class="form-control" th:name="selectEstado" th:id="selectEstado" required> <option value="" selected="selected">Change status</option> <th:block th:each="i : ${statuses}"> <th:block th:if="${i != spot.status}"> <option th:value="${i}" th:text="${i}"></option> </th:block> </th:block> </select> </th:block> 获取表名的结果,以编程方式构建新的查询字符串。像您提到的那样对每个表进行一次查询,或者通过创建类似

的表来进行查询
sqlite_master

您没有提到您使用的是哪种语言,因此请以一种具有功能性风格的伪代码编写代码:

SELECT 'table1' AS Tablename, count(*) AS Records FROM table1
UNION ALL
SELECT 'table2', count(*) FROM table2
-- etc.