I have two tables. One that keeps all of my threads started on a subject and another that keeps all the posts that related to each thread. They are both using the “id” columns for relation purposes. For example the very first thread (AKA subject) opened up has three comments total on it including the original post. For this example I have taken the irrelevant columns and named them consecutive column names such as col1-col6. I do still need all of these columns returned in the query results though!
table – dbo.Threads (parent table)
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 (child table)
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
Currently I have tried using the following query just for testing…
"SELECT Threads.*, Posts.* FROM Threads INNER JOIN Posts ON Threads.id = Posts.id ORDER BY Posts.timestamp"
This returns the following…
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
I need to have a query that will search both the dbo.Threads.subject column and the dbo.Posts.post column for a text crietera. In this example I will use “blue”. Currently I have tried using the following query…
"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"
This returns the following…
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
That is ALMOST exactly what I needed as the results… I currently need it to return the same exact results but without the last two lines. I only want unique id’s returned. In this case I do not want three results for id “1” to be returned, I only want one result for “1” to be returned and then of course one results returned for “3” giving me the following results…
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
Any ideas?
–The and statement here is intended to get the earliest timestamp for the Thread.ID