本文介绍了将两个不相关查询的结果合并到单个视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将两个单独的(不相关的)sql查询的结果合并到一个视图中.我正在尝试汇总一些用户数据并计算本月视频的观看次数,以显示在仪表板上.
Is it possible to combine the results of two separate (unrelated) sql queries into a single view. I am trying to total some figures for users and count the views for videos this month to display on a dashboard.
即
select count(*) from video where monthname(views) = 'May';
和
select sum(sessions) from user where user_id = 6;
我想创建一个包含这两个结果的视图.
I would like to create a view that combines that contains these two results.
这可能吗?
推荐答案
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中的输出均为一行时,这才非常有效.
Ofcourse, this will be very efficient only when the output in both t1 and t2 is one row.
这篇关于将两个不相关查询的结果合并到单个视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!