0

I like to make an forum teaser for my website. Its easy to just show the latest posts or threads.. I like to get latest threads and posts in the same query, ordered by the last activity. So its going to be ordered by REPLY TO POST date, and THREAD POST date in the same query. I think it has some think to do with how you GROUP it, but I'm not sure.

Tables

threads

id, header, text, date, author

posts

id, text, date, author, thread_id

Example of usage

20 minutes ago - How to make an php/mysql script (2)

17 minutes ago - Pls help me out here (0)

1 hour ago - I need help with PHP (1)

As you see, both answered threads and new threads are on the list. (I need a date of the latest reply or when it was created, header and a count() of replys)

I hope you get, and know how to do this.

Troels

UPDATE:

I have this, and its okay, but i only get threads with replys.

SELECT 
    threads.*, 
    posts.*, 
    (SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate, 
    (SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys 
FROM 
    threads, 
    posts 
WHERE 
    threads.id = posts.thread_id
GROUP BY 
    thread_id
ORDER BY 
    postdate DESC, 
    thread.date 
LIMIT 
    0,15

HOW CAN I DO THIS?

UPDATE

aaaaaaaaaaaaaaaaawwwww Yeah!!!!

I managed to do it myself :-) Took a while to get it right.

SELECT 
    fisk_debat.id, 
    fisk_debat.dato,
    IF((SELECT count(id) FROM fisk_debat_svar WHERE debatid = fisk_debat.id) < 1, fisk_debat.dato, (SELECT dato FROM fisk_debat_svar WHERE debatid = fisk_debat.id ORDER BY dato DESC LIMIT 0,1)) AS svardato,
    fisk_debat.overskrift,
    (
    SELECT count(fisk_debat_svar.debatid) 
    FROM fisk_debat_svar 
    WHERE fisk_debat_svar.debatid = fisk_debat.id
    ) AS svar

FROM  fisk_debat
GROUP BY  id
UNION
SELECT
    fisk_debat_svar.debatid AS id, 
    max(fisk_debat_svar.dato) AS dato,
    max(fisk_debat_svar.dato) AS svardato,
    (
    SELECT fisk_debat.overskrift 
    FROM fisk_debat 
    WHERE fisk_debat.id = fisk_debat_svar.debatid
    ) AS overskrift,
    (
    SELECT count(fisk_debat_svar.debatid) 
    FROM fisk_debat_svar 
    WHERE fisk_debat_svar.debatid = id
    ) AS svar

FROM fisk_debat_svar
WHERE id != id
GROUP BY id

ORDER BY svardato DESC, dato DESC
LIMIT 0,15
EastDane
  • 137
  • 1
  • 10
  • Have you developed the forum software yourself? The DB structure looks flawed, the 'original post' in a thread should also be in the **posts** table. It would also make your query easier. – user247702 May 16 '11 at 11:50
  • Hey. Yep, I have developed it myself. And the original posts should be there to. – EastDane May 16 '11 at 11:52
  • Without checking your entire query, you have a typo on the 4th line. **(SELECT dato** should be **(SELECT date** – user247702 May 16 '11 at 12:06
  • Just a little miss. :-) I made the names easy for you guys. Its not original table, row names. – EastDane May 16 '11 at 12:09

2 Answers2

0

If you want to keep the current DB structure, you'll need a Union to get the desired result. An example can be found at http://www.mysqltutorial.org/sql-union-mysql.aspx

However, I'd still advise to change the structure as explained in the comments to your question.

user247702
  • 23,641
  • 15
  • 110
  • 157
0

WHERE clauses only select threads with replies, which is normal. You have to use the LEFT JOIN syntax.

Try this:

SELECT 
    threads.*, 
    posts.*, 
    (SELECT date FROM posts WHERE thread_id = threads.id ORDER BY date DESC LIMIT 0,1) AS postdate, 
    (SELECT count(id) FROM threads WHERE thread_id = thread.id) AS replys 
FROM 
    threads
LEFT JOIN
    posts
ON
    threads.id = posts.thread_id
ORDER BY 
    postdate DESC, 
    thread.date 
LIMIT 
    0,15
Phen
  • 2,313
  • 3
  • 18
  • 15
  • Well it does not seem to work out. I need the date from the original thread too. IF their isnt any replys. – EastDane May 16 '11 at 12:43