i was looking for a solution; the query is:
SELECT MAX(forum_commenti.Data) AS MData, forum_post.id AS id, forum_post.Nome AS Nome, forum_post.Messaggio AS Messaggio, forum_post.Sezione AS Sezione, forum_post.Data AS Data, forum_post.Utente AS Utente, forum_post.Chiuso AS Chiuso, forum_post.Importante AS Importante
FROM forum_post LEFT OUTER JOIN forum_commenti ON forum_post.id = forum_commenti.Post
WHERE forum_post.Importante = 0
AND forum_post.Sezione = '".$_GET['id']."'
GROUP BY id, Nome, Messaggio, Sezione, Data, Utente, Chiuso, Importante
ORDER BY MData IS NOT NULL DESC, Data DESC
LIMIT $start, $per_page
This is a query for a forum; i'm trying to display the order of posts for Data. My want would be:
- If a post haven't replys, order that post using his own date, else use the date of the last comment for that post. (other informations are useless)
I looked for old questions solved about something like this but it gave me problem when i try to do like:
ORDER BY MData IS NOT NULL DESC, Data ASC
It says "Reference 'MData' not supported (reference to group function)".
I'm using that query for a php function.
The tables involved in the query are:
- forum_post : Contains all the posts of the forum
- forum_commenti : Contains all the replys of all posts, using 'Post' as a foreign key of forum_post
The WHERE condition is a useless point for the query.
I'll show you the example:
POST 1 | Data of the last reply is 19/12/2014 10:00:00 , Data of post is 19/12/2014 09:00:00 TAKE Data of the last comment
POST 2 | Data of the last reply is NULL (hasen't got replys), Data of post is 19/12/2014 08:00:00 TAKE Data of the post
....... ....... etc
Now that i have something like that:
POST 1 | 19/12/2014 10:00:00
POST 2 | 19/12/2014 08:00:00
ORDER BY Data
Thanks you all for helping.