Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine results of two unrelated queries into single view

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.

i.e.,

select count(*) from video where monthname(views) = 'May';

and

select sum(sessions) from user where user_id = 6;

I would like to create a view that combines that contains these two results.

Is this possible?

like image 551
M Azam Avatar asked May 16 '14 17:05

M Azam


1 Answers

If you want the results next to each other in separate columns you can simply SELECT a list of queries:

SELECT ( select count(*) from video where monthname(views) = 'May') AS May_CT
      ,( select sum(sessions) from user where user_id = 6) AS User_Sum

If you want the results stacked in one column:

select count(*) from video where monthname(views) = 'May'
UNION  ALL
select sum(sessions) from user where user_id = 6

The latter may require datatype conversion

like image 181
Hart CO Avatar answered Sep 21 '22 00:09

Hart CO