是否可以将两个单独的(不相关的)SQL查询的结果合并到一个视图中。我正在尝试汇总一些用户数据并计算本月视频的观看次数,以显示在仪表板上。
IE,
select count(*) from video where monthname(views) = 'May';
和
select sum(sessions) from user where user_id = 6;
我想创建一个包含这两个结果的视图。
这可能吗?
SELECT t2.total_session, t1.watch_count FROM (SELECT 1 AS common_key, count(*) AS watch_count FROM video WHERE monthname(views) = 'May') AS t1 JOIN (SELECT 1 AS common_key, sum(sessions) AS total_session FROM USER WHERE user_id = 6) AS t2 ON t1.common_key = t2.common_key;
当然,仅当t1和t2中的输出均为一行时,这才是非常有效的。