我对SQL很陌生,但鉴于以下示例,有没有办法压缩查询?根据我的理解,SQL没有数组,但是可以以某种方式存储表并执行一个SELECT语句,可能是一个变量(从Table_Name中选择Count(*))作为Table_Name)?
SELECT
(SELECT COUNT(*) FROM ActivityLog) AS ActivityLog,
(SELECT COUNT(*) FROM ActivityLogin) AS ActivityLogin,
(SELECT COUNT(*) FROM ActivityPlayContent) AS ActivityPlayContent,
(SELECT COUNT(*) FROM ChangeLog) AS ChangeLog,
(SELECT COUNT(*) FROM ContentApprovalLog) AS ContentApprovalLog,
(SELECT COUNT(*) FROM ExceptionLog) AS ExceptionLog,
(SELECT COUNT(*) FROM State) AS State
答案 0 :(得分:2)
如果您经常需要此查询并希望更简单,则可以创建VIEW。 然后,您必须像查询表一样查询视图。
SELECT * FROM MyView
当您有多个复杂查询并希望在其他查询中重复使用它们时,视图尤其有用(例如,将两个视图连接在一起或子视图中)。
为您的查询创建视图,如下所示
CREATE VIEW MyView AS
SELECT
(SELECT COUNT(*) FROM ActivityLog) AS ActivityLog,
(SELECT COUNT(*) FROM ActivityLogin) AS ActivityLogin,
(SELECT COUNT(*) FROM ActivityPlayContent) AS ActivityPlayContent,
(SELECT COUNT(*) FROM ChangeLog) AS ChangeLog,
(SELECT COUNT(*) FROM ContentApprovalLog) AS ContentApprovalLog,
(SELECT COUNT(*) FROM ExceptionLog) AS ExceptionLog,
(SELECT COUNT(*) FROM State) AS State