两个具有链接列“id”的表,它们只返回父表的唯一“id”行

时间:2011-11-10 13:58:52

标签: sql

我有两张桌子。一个让我的所有线程都保持在一个主题上,另一个保持与每个线程相关的所有帖子。它们都使用“id”列进行关联。例如,打开的第一个主题(AKA主题)总共有三条评论,包括原始帖子。对于这个例子,我采用了不相关的列,并将它们命名为连续的列名,例如col1-col6。我仍然需要在查询结果中返回所有这些列!

table - dbo.Threads (父表)


id  col1  timestamp                col2  col3  col4  col5  subject
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue
2   7     2011-11-07 14:53:01.410  6     NULL  2     0     Request 2 blah green
3   6     2011-11-07 14:54:01.453  6     NULL  3     0     Request 3 blah red
4   7     2011-11-08 10:50:57.440  6     NULL  3     0     Request 4 blah black

table - dbo.Posts (子表)


id  timestamp                  col6  post
1   2011-11-07 14:52:08.710    3     This is the 1st orange post for one
2   2011-11-07 14:53:01.420    3     This is the 1st green post for two
3   2011-11-07 14:54:01.463    3     This is the 1st blue post for three
1   2011-11-08 15:08:59.707    3     This is the 2nd red post for one
1   2011-11-09 15:09:16.333    3     This is the 3rd black post for one
4   2011-11-08 10:50:57.527    3     This is the 1st yellow post for four

目前我已尝试使用以下查询进行测试......


"SELECT Threads.*, Posts.* FROM Threads INNER JOIN Posts ON Threads.id = Posts.id ORDER BY Posts.timestamp"

返回以下内容......


id  col1  timestamp                col2  col3  col4  col5  subject               id  timestamp                col6  post
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue   1   2011-11-07 14:52:08.710  3     This is the 1st orange post for one
2   7     2011-11-07 14:53:01.410  6     NULL  2     0     Request 2 blah green  2   2011-11-07 14:53:01.420  3     This is the 1st green post for two
3   6     2011-11-07 14:54:01.453  6     NULL  3     0     Request 3 blah red    3   2011-11-07 14:54:01.463  3     This is the 1st blue post for three
4   7     2011-11-08 10:50:57.440  6     NULL  3     0     Request 4 blah black  4   2011-11-08 10:50:57.527  3     This is the 1st yellow post for four
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue   1   2011-11-08 15:08:59.707  3     This is the 2nd red post for one
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue   1   2011-11-09 15:09:16.333  3     This is the 3rd black post for one

我需要有一个查询,它将搜索文本crietera的dbo.Threads.subject列和dbo.Posts.post列。在这个例子中,我将使用“蓝色”。目前我已尝试使用以下查询...


"SELECT Threads.*, Posts.* FROM Threads INNER JOIN Posts ON Threads.id = Posts.id WHERE ((Threads.subject LIKE '%blue%') OR (Posts.post LIKE '%blue%')) ORDER BY Posts.timestamp"

返回以下内容......


id  col1  timestamp                col2  col3  col4  col5  subject              id  timestamp                col6  post
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue  1   2011-11-07 14:52:08.710  3     This is the 1st orange post for one
3   6     2011-11-07 14:54:01.453  6     NULL  3     0     Request 3 blah red   3   2011-11-07 14:54:01.463  3     This is the 1st blue post for three
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue  1   2011-11-08 15:08:59.707  3     This is the 2nd red post for one
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue  1   2011-11-09 15:09:16.333  3     This is the 3rd black post for one

这几乎就是我需要的结果...我目前需要它返回相同的确切结果但没有最后两行。我只想要返回唯一的id。在这种情况下,我不希望返回id为“1”的三个结果,我只想要返回一个“1”的结果,然后当然一个结果返回“3”给我以下结果... < / p>


id  col1  timestamp                col2  col3  col4  col5  subject              id  timestamp                col6  post
1   6     2011-11-07 14:52:08.650  6     NULL  3     0     Request 1 blah blue  1   2011-11-07 14:52:08.710  3     This is the 1st orange post for one
3   6     2011-11-07 14:54:01.453  6     NULL  3     0     Request 3 blah red   3   2011-11-07 14:54:01.463  3     This is the 1st blue post for three

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

SELECT T.*, P.* 
FROM Threads 
INNER JOIN POSTS P ON T.id = P.id 
WHERE ((T.subject LIKE '%blue%') OR (P.post LIKE '%blue%')) 
AND P.Timestamp = (Select min(P2.timestamp) from posts P2 where ID = T.ID) 
ORDER BY P.timestamp

- 此处的and语句旨在获取Thread.ID

的最早时间戳

答案 1 :(得分:1)

单向;

  SELECT * FROM (
    SELECT 
        T.id, T.timestamp, T.subject,
        P.id as POST_ID, P.timestamp as POST_TIMESTAMP, P.post,
        ROW_NUMBER() OVER(PARTITION BY T.id ORDER BY T.id, P.timestamp) AS ROWNUM
    FROM 
        Threads as T INNER JOIN Posts AS P ON (T.id = P.id)
    WHERE
        T.subject LIKE '%blue%' OR P.post LIKE '%blue%'
 ) X
    WHERE X.ROWNUM = 1

这里我们根据唯一的id进行分区,根据线程ID的顺序和帖子的时间戳,为每一个生成一个序数行号。