I have 4 columns, let's say A, B, C and D. Is there an easy way to create a where clause that will check if any of these 4 have a different value? In my case, every column has to be the same value, but if any of these 4 are not, the query must return the record. I know there is a hard way with OR, but is there an more gentle way?
AND
, instead of OR
?
SQL> with test (a, b, c, d) as
2 (select 1, 2, 2, 4 from dual union all
3 select 2, 2, 2, 2 from dual union all
4 select 1, 1, 1, 4 from dual
5 )
6 select *
7 from test
8 where not (a = b and b = c and c = d);
A B C D
---------- ---------- ---------- ----------
1 2 2 4
1 1 1 4
SQL>
I'd probably go with @Littlefoot's approach
where not( a = b and b = c and c = d )
The other approach would be to use the greatest
and least
functions
where greatest(a, b, c, d) != least(a, b, c, d)
Of course, if any of your columns are nullable, you may have to tweak the logic depending on what you want to happen since null
is neither equal to nor unequal to any other value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.