I'm moving our backend database from mysql to postgres and am in the process of migrating all of our old queries / functions. Most of them are trivial to do, but today I ran across one that has me scratching my head. Here it is:
UPDATE
k_coderound AS cr, k_coderound AS cr_m
SET
cr.is_correct = IF(
(SELECT GROUP_CONCAT(option_id ORDER BY variable_id) AS `values` FROM k_value
WHERE code_round_id=cr.id GROUP BY code_round_id) =
(SELECT GROUP_CONCAT(option_id ORDER BY variable_id) AS `values` FROM k_value
WHERE code_round_id=cr_m.id GROUP BY code_round_id),
1,
0
)
WHERE
cr.is_master=0
AND cr_m.is_master=1
AND cr_m.object_id=cr.object_id
AND cr_m.content_type_id =cr.content_type_id
I know that Postgres has no group_concat and one should instead use array_agg. My problem is that I can't figure out what is going on exactly- this query was written ages ago by someone who isn't with us anymore. Also compounding this difficult is the lack of the IF statement in Postgres. If anyone is able to provide feedback or advice I'd greatly appreciate it!