SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name
FROM messages AS mess
INNER JOIN `users`
ON users.id = mess.user_id
WHERE mess.parent_id = 3
GROUP BY mess.created
SELECT mess.subject, mess.message_txt, mess.mess_id, mess.parent_id, mess.created, users.name,
COUNT(rply.parent_id) AS replies
FROM messages AS mess
INNER JOIN `users`
ON users.id = mess.user_id
LEFT OUTER JOIN messages AS rply
ON rply.parent_id = mess.mess_id
WHERE mess.topic_id = 1
AND mess.parent_id IS NULL
GROUP BY mess.mess_id
(left joins are confusing to me), because I dont want to have 2 queries I think ill need 1.
The screenshot is the messages table of which the message im looking for has a parent_id of NULL and a topic_id of 1,
Here the query i think i need to add the original post to with a left join of some kind
Left joins are simply saying “I want everything from table A, regardless of if there is matching data in table B. But if there is matching data in table B, i want that too.”
(Right joins are the same, but invert A and B)
Inner joins are: Show me things where there is matching data in A and B.
The way generally forums work is that they seperate the logic of ‘parent’ from ‘topic’. All posts simply belong to a singular topic (thread, whatever), and so all messages in a given thread can be referenced as SELECT stuff FROM posts WHERE thread = somethreadid ORDER BY DATE (since noone can post into a thread before the original post in the thread because that’s how linear time works in our dimension of understanding.)