Use SQL quote helpers so the bulk operations work in postgres as well as mysql.
You need to GROUP BY all non aggregate functions