0

我对四个不同的表有四个 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 个中设置结果。

我不能使用联合,因为列不同。

所以唯一的问题是加入结果集。

4

1 回答 1

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 相同的方法。

于 2012-08-09T12:10:11.413 回答