题目描述
针对库中的所有表生成select count(*)对应的SQL语句,如数据库里有以下表,
(注:在 SQLite 中用 “||” 符号连接字符串,无法使用concat函数)
employees
departments
dept_emp
dept_manage
salaries
titles
emp_bonus
那么就会输出以下的样子:
| cnts |
|---|
| select count(*) from employees; |
| select count(*) from departments; |
| select count(*) from dept_emp; |
| select count(*) from dept_manager; |
| select count(*) from salaries; |
| select count(*) from titles; |
| select count(*) from emp_bonus; |
解答
在 SQLite 系统表 sqlite_master 中可以获得所有表的索引,其中字段 name 是所有表的名字,而且对于自己创建的表而言,字段 type 永远是 ‘table’
select "select count(*) from " || name || ";" as cnts
from sqlite_master
where type = 'table'
在MySQL中的写法:
关于其中的information_schema.tables 可以参考这篇文章
select concat('select count(*) from ', table_name, ';') as cnts
from (
select table_name
from information_schema.tables
where table_schema = 'my_test'
);

1858

被折叠的 条评论
为什么被折叠?



