1

I'm wondering if it's possible to do a bitwise OR among MySQL query. I have a query like:

SELECT `value` FROM `table` WHERE `code`='4'

It returns an array of values, but I want a unique value with the bitwise OR of all values. Is is possible from MySQL or should I delegate to PHP?

I.e. The query above returns this list of values:

value
-----
1
5
4
2
7
8

And I want as a result the OR of these values 1 | 5 | 4 | 2 | 7 | 8 = 15

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ivan
  • 14,692
  • 17
  • 59
  • 96

1 Answers1

6
SELECT MAX(@r:=@r|value) FROM `table`, (SELECT @r:=0) x

Jim.H's suggestion is much better. Thanks!

SELECT BIT_OR(value) FROM `table`
lqez
  • 2,898
  • 5
  • 25
  • 55