Frage

Currently I'd have to write a query like this

SELECT * FROM table WHERE value1 > 5000 OR value2 > 5000 OR value3 > 5000 OR value4 > 5000

As you can see, the 5000 is compared to all of those columns. Ideally, you'd be able to write a query like this

SELECT * FROM table WHERE 5000 GREATER(value1,value2,value3,value4)

I know you can do

SELECT * FROM table WHERE 5000 IN(value1,value2,value3,value4)

But that tests for equality, not greater than.

Is there a way to do what I'm looking for?

War es hilfreich?

Lösung

You could do:

SELECT *
FROM table
WHERE greatest(value1, value2, value3, value4, value5) > 5000 ;

As a note, this is not exactly the same. If any of the values are NULL this will return NULL, which is treated as false. The version with or will skip NULL values.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top