Hi everyone!
I have a forum on my site and I need a hint how with a simple query to achieve the desired result.
I have three tables:
forums
- ForumID - int
threads
- ForumID - int
- ThreadID - int
- Sticky - int
- ThreadDateStamp - int
texts
- TextDateStamp - int
Now on the screen where I display the list of threads in a certain forum I want to sort the threads by the following criterias: threads.Sticky, texts.TextDateStamp, threads.ThreadDateStamp.
The best query I could think of was:
SELECT *, texts.TextDateStamp
FROM threads
LEFT JOIN texts ON threads.ThreadID = texts.ThreadID
WHERE ForumID = '$ForumID'
ORDER BY Sticky DESC,texts.TextDateStamp DESC,ThreadDateStamp DESC
However this does not return the desired result since there could be more than one row in texts that corresponds to a row in threads and I get duplicated row. Adding DISTINCT after SELECT did not help. I added GROUP BY ThreadID after WHERE but then the result was not sorted by TextDateStamp as I needed it to be.
Could anyone suggest anything?
MySQL version is 3.23.49
I have a forum on my site and I need a hint how with a simple query to achieve the desired result.
I have three tables:
forums
- ForumID - int
threads
- ForumID - int
- ThreadID - int
- Sticky - int
- ThreadDateStamp - int
texts
- TextDateStamp - int
Now on the screen where I display the list of threads in a certain forum I want to sort the threads by the following criterias: threads.Sticky, texts.TextDateStamp, threads.ThreadDateStamp.
The best query I could think of was:
SELECT *, texts.TextDateStamp
FROM threads
LEFT JOIN texts ON threads.ThreadID = texts.ThreadID
WHERE ForumID = '$ForumID'
ORDER BY Sticky DESC,texts.TextDateStamp DESC,ThreadDateStamp DESC
However this does not return the desired result since there could be more than one row in texts that corresponds to a row in threads and I get duplicated row. Adding DISTINCT after SELECT did not help. I added GROUP BY ThreadID after WHERE but then the result was not sorted by TextDateStamp as I needed it to be.
Could anyone suggest anything?
MySQL version is 3.23.49