Combine results of two unrelated queries into single view

前端 未结 2 711
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:08

    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

提交回复
热议问题