Combine results of two unrelated queries into single view

前端 未结 2 721
Happy的楠姐
Happy的楠姐 2021-01-03 02:34

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 03:34

    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;
    

    Ofcourse, this will be very efficient only when the output in both t1 and t2 is one row.

提交回复
热议问题