结合许多选择查询的结果集

时间:2012-08-09 11:06:51

标签: sql sql-server sql-server-2005

我对四个不同的表有四个Select查询,每个表只提取一个记录。例如:

Select * from table where col1 = 'something'

给出一行有3列。

第二个选择查询还会给出一个包含两列(字段)的记录。对于第三和第四选择查询也是如此。

我想将所有四个结果集合并为一行。怎么可能?

我会为你写查询。

第一名:

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

第二次查询:

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

第三个查询使用从查询2获得的结果:

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

第四:

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

这意味着我必须加入第2,第3,第4个查询,然后将结果设置为第1个。

由于列不同,我无法使用union。

因此,唯一的问题是加入结果集。

1 个答案:

答案 0 :(得分:0)

您可以使用CROSS JOIN来完成此任务。

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

结果:

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

澄清后更新:

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

对table4使用与table3相同的方法。