combining resultset of many select queries

这一生的挚爱 提交于 2019-12-12 04:11:45

问题


I have four Select queries for four different tables, each extracting only one record. For example:

Select * from table where col1 = 'something'

gives one row having 3 columns.

The second select query also gives one record having two columns(fields). Same for third and fourth select query.

I want to combine all four result sets into one having one row. How is it possible?

I will write the queries for you.

1st one:

Select Top 1 column1, column2
from table 1
where column 1 = 'something'
  and col1 = (Select max(col1) where column 1 = 'something')

2nd query:

Select Top 1 column1, column3
from table 2
where column 1 = 'something'
  and column3 = (Select max(column3) where column 1 = 'something')   

3rd query uses the result obtained from query 2:

Select column4
from table 3
where column3 = (obtained from 2nd query)  (there is only one row)

4th:

Select column5
from table 4
where column3 = (obtained from 2nd query) (there is only one row)

This means I have to join 2nd, 3rd, 4th query, then resulting set in 1st.

I can't use union since columns are different.

So only problem is with joining the result set.


回答1:


You can use CROSS JOINs to accomplish this.

CREATE TABLE table1 (id int, column1 varchar(5), column2 varchar(15));

CREATE TABLE table2 (column3 varchar(5), column4 varchar(15));

CREATE TABLE table3 (id int, column5 varchar(5), column6 varchar(15));

INSERT INTO table1 VALUES (1, 'aaa', 'row1')
INSERT INTO table2 VALUES ('bbb', 'table2')
INSERT INTO table3 VALUES (1, 'ccc', 'table3')
INSERT INTO table1 VALUES (1, 'ddd', 'table1')

SELECT * FROM (SELECT * FROM table1) a
           CROSS JOIN (SELECT * FROM table2) b 
           CROSS JOIN (SELECT * FROM table3) c

Result:

id    column1    column2   column3    column4    id    column5    column6
1     aaa        row1      bbb        table2     1     ccc        table3
1     ddd        table1    bbb        table2     1     ccc        table3

Update after clarification:

CREATE TABLE table1 
(
        id int IDENTITY(1,1) 
        , searchstring nvarchar(25)
);

CREATE TABLE table2 
(
        id2 int IDENTITY(10, 10) 
        , searchstring2 nvarchar(25)
        , newsearchstring nvarchar(50)
);

CREATE TABLE table3
(
        id3 int IDENTITY(100, 100) 
        , id2 int
        , table3srow nvarchar(25)
)

INSERT INTO table1 VALUES ('something');
INSERT INTO table1 VALUES ('something else');
INSERT INTO table1 VALUES ('something'); -- ID = 3, this row will be selected by 1st query

INSERT INTO table2 VALUES ('something', 'newvalue1');
INSERT INTO table2 VALUES ('something else', 'this will not be shown');
INSERT INTO table2 VALUES ('something', 'this will be returned by query 2'); -- ID = 30, this row will be selected by 2nd  query

INSERT INTO table3 VALUES (10, 'not relevant');
INSERT INTO table3 VALUES (20, 'not relevant');
INSERT INTO table3 VALUES (30, 'This is from table 3'); -- This  row will be returned by 3rd query


SELECT * FROM 
    (SELECT TOP 1 id, searchstring FROM table1 WHERE searchstring = 'something'  and id =     (SELECT MAX(id) FROM table1 WHERE searchstring = 'something')) AS  query1,
    (SELECT TOP 1 id2, newsearchstring FROM table2 WHERE searchstring2 = 'something'  and id2 = (SELECT MAX(id2) FROM table2 WHERE searchstring2 = 'something')) AS  query2,
    (SELECT id2, table3srow FROM table3) as query3 
WHERE query3.id2 = query2.id2

Use the same approach for table4 as indicated for table3.



来源:https://stackoverflow.com/questions/11882039/combining-resultset-of-many-select-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!