I am receiving the following error from the below query, as I am trying to order the results by a value being retrieved in the query itself. Please can you tell me how I can get around this error, I am guessing by creating a sub-query within the query, of which I don’t know how to!
SELECT q.*, COUNT(DISTINCT a.qid) AS `a_count`
FROM `questions` AS q
INNER JOIN `answers` AS a
ON a.qid = q.id
ORDER BY MAX(a_count)
LIMIT 0, 10;
You can’t
ORDER BY MAX(a_count), as that doesn’t make sense. (You can’t really sort by a single value)You probably wanted to do
ORDER BY a_count.