I am using an external DB and I have 3 important columns: user_id, total_score, score_order.
I would like to get the total_score of each user.
All the scores are always recorded, so I only need the last one. For this I need to use the score_order column.
This is what I am trying to do (using nested queries because I need to combine ORDER BY and GROUP BY):
SELECT * FROM (
SELECT * FROM `table` ORDER BY score_order DESC
) AS tmp_table GROUP BY user_id
But I get the error:
'#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tmp_table.ranking_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by Can someone explain what I am doing wrong?'
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `table` ADD INDEX `table_idx_score_order` (`score_order`);
SELECT
*
FROM
(SELECT
*
FROM
`table`
ORDER BY
`table`.score_order DESC) AS tmp_table
GROUP BY
tmp_table.user_id
ORDER BY
NULL