Take the 2-minute tour ×

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:


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

share|improve this question
Duplicate or similar to: stackoverflow.com/questions/1397913/… –  Jim H. Nov 9 '11 at 16:22
SQL queries do not return "arrays". –  Lightness Races in Orbit Nov 9 '11 at 16:46
add comment

1 Answer

up vote 6 down vote accepted
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`
share|improve this answer
Clever, but eww. SELECT bit_or(value) FROM table; –  Jim H. Nov 9 '11 at 16:28
add comment

Your Answer


