I have a table with the following columns: user_id, key, value, and flag. The SELECT, FROM, WHERE, GROUP BY and ORDER BY work by themselves as expected. It’s when I try to add the key to the table, I get an errors that says to check my syntax. The key is the question and the value are the answers. I want a result that shows all the questions with all possible answers and a count of how often that answer occurred.
SELECT value AS 'Answer', count(*) AS 'Total Votes'
FROM survey_data sd
INNER JOIN (SELECT key FROM sd GROUP BY key) T1 ON T1.key = sd.key
WHERE flag != 1
GROUP BY value
ORDER BY 'Total Votes' DESC
What am I missing here?
As a side note, when I run this query without the INNER JOIN, the result is not ordered DESC by ‘Total Votes’ — does anyone know why or what the problem might be?
Thanks,
Ryan
key is a reserved word.
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
put it within backticks (alt+96) or rename your field
edit. If you use an alias with more than one word you have to use backtick too.
So change your order by clause in
order by
total votesdesc without quotesPut backtick before and after total votes. I don’t see them in my post or even better change your field alias with a single word.