0

I am trying to use an SQL query in Postgres that looks like this:

UPDATE gamebet 
SET points = 15 
WHERE game = 8 
  AND "scoreT1" > "scoreT2" 
  AND "scoreT1" - "scoreT2" != 1 
  AND ("scoreT1" != 1 AND "scoreT2" != 0)

It should update the scores of some bets where scoreT1 is bigger than scoreT2, but there are some rows that should not be updated. And that is when scoreT1 - scoreT2 = 1 And if both scoreT1 = 1 and scoreT2 = 0, but it should only apply if both of these conditions are met. And somehow the parenthesis are not applied. Since e.g. this record is not modified:

scoreT1 = 3
scoreT2 = 0

This record should be updated, but since one of the conditions is scoreT2 != 0 it is not updated. How can I group the last conditions that they need to be met together?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
m0c
  • 2,180
  • 26
  • 45

2 Answers2

0

From your description you want

 NOT("scoreT1" - "scoreT2" == 1 OR ("scoreT1" == 1 AND "scoreT2" == 0))

Which logically would be

 "scoreT1" - "scoreT2" != 1 AND ("scoreT1" != 1 OR "scoreT2" != 0)

When you commute a NOT operator into a compound comparison you flip ANDs to ORs and vice-versa.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

As long as NULL values are not ruled out, your description must be translated to something like this:

UPDATE gamebet 
SET    points = 15 
WHERE  game = 8 
AND   "scoreT1" > "scoreT2" 
AND  ("scoreT1" = "scoreT2" + 1 AND "scoreT1" = 1) IS NOT TRUE;

The additional condition "scoreT2" = 0 follows logically from the first two and is redundant.

If "scoreT1" and "scoreT2" are defined NOT NULL, you can further simplify:

...
AND   NOT ("scoreT1" = "scoreT2" + 1 AND "scoreT1" = 1)

Or with inverted logic:

...
AND   ("scoreT1" <> "scoreT2" + 1 OR "scoreT1" <> 1)

Read about Logical Operators, Comparison Operators and Operator Precedence in the manual.

And if points can already be 15, it pays to add another predicate to avoid empty updates:

AND points IS DISTINCT FROM 15

Or, with NULL values ruled out:

AND points <> 15

Details:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228