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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top