Combining the results of two SQL queries as separate columns

前端 未结 4 1256
轮回少年
轮回少年 2020-11-27 16:32

I have two queries which return separate result sets, and the queries are returning the correct output.

How can I combine these two queries into one so that I can

4条回答
  •  一向
    一向 (楼主)
    2020-11-27 17:24

    You could also use a CTE to grab groups of information you want and join them together, if you wanted them in the same row. Example, depending on which SQL syntax you use, here:

    WITH group1 AS (
      SELECT testA
        FROM tableA
    ),
    group2 AS (
      SELECT testB
        FROM tableB 
    )
    SELECT *
      FROM group1
      JOIN group2 ON group1.testA = group2.testB --your choice of join
    ;
    

    You decide what kind of JOIN you want based on the data you are pulling, and make sure to have the same fields in the groups you are getting information from in order to put it all into a single row. If you have multiple columns, make sure to name them all properly so you know which is which. Also, for performance sake, CTE's are the way to go, instead of inline SELECT's and such. Hope this helps.

提交回复
热议问题