I am getting an error in a query:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group by `messages`.`to_id` UNION SELECT `users`.`username` as 'subj' at line 13
I didn't write the query, and not sure what union does so not sure how to fix it.
select `thread`.`subject`,
`thread`.`id`,
max(`thread`.`mostrecent`) as 'mostrecent',
sum(`thread`.`from` + `thread`.`to`) as 'messages'
from (
SELECT `users`.`username` as 'subject',
`messages`.`to_id` as 'id' ,
max(`messages`.`created`) as 'mostrecent',
count(*) as 'from',
0 as 'to' from `messages`
join `users` on `messages`.`to_id` = `users`.`id`
where `messages`.`from_id` = $id
group by `messages`.`to_id`
UNION
SELECT `users`.`username` as 'subject',
`messages`.`from_id` as 'id',
max(`messages`.`created`) as 'mostrecent',
0 as 'from',
count(*) as 'to' from `messages`
join `users` on `messages`.`from_id` = `users`.`id`
where `messages`.`to_id` = $id
group by `messages`.`from_id`
) as thread group by `thread`.`subject` order by max(`thread`.`mostrecent`) desc