简体   繁体   中英

PHP MYSQL Forum teaser, howto?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM