Skip to content
Advertisement

query to update records count based on relation and constraints

Im trying to update a column (address_count) on my people table that must have the sum of records from another table (people_addresses) plus some constraints.

The idea is that, the record on people must exist on people_addresses and must be present on at least one of the other tables (table a, table b)

With the query above, I get the expected count, for each address that fits my requirements, altough, when I try to use it on a update, I get weird results, the records that don’t fit the requirements end up with wrong values on people_count

query im using:

I get the correct result, on records that fit the requirements, but wrong values on the ones that dont fit, when I run the update.

Advertisement

Answer

The problem is in your conditions:

Is actually equivalent to:

You need to surround the OR condition with parentheses to make your intent explicit:

Also, the GROUP BY clause is superfluous in the subquery. I would suggest removing it (you will get a 0 value if the subquery does not match).

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement