mercredi 24 février 2016

GROUP BY HAVING: SQLite vs MySQL

For some reason MySQL does not accept the following query:

SELECT `tasks`.* FROM `tasks` INNER JOIN `task_status_updates` ON `task_status_updates`.`task_id` = `tasks`.`id` GROUP BY task_status_updates.task_id HAVING `task_status_updates`.`status` = 0

Running the exact same query in SQLite, however, produces the expected results (A list of tasks whose last task_status_update has the status 0). This error just happened on the production environment of Rails, because of the difference in SQLite and MySQL.

MySQL throws the following error:

ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'task_status_updates.status' in 'having clause'

Could someone explain why above statement is invalid in MySQL, and how the wanted result can be obtained in a way that MySQL understands?

Aucun commentaire:

Enregistrer un commentaire