MySQL BIT_OR() Function

The MySQL BIT_OR() function is an aggregate function that performs a “bitwise OR” of all non-null input values.

The bitwise OR processes two binary numbers of the same length, if the two corresponding binary bits are 0, the result value of the bit is 0, otherwise it is 1.

BIT_OR() Syntax

Here is the syntax for MySQL BIT_OR() function:

BIT_OR(expr)

We usually use the BIT_OR() function like this:

SELECT BIT_OR(expr), ...
FROM table_name
[WHERE ...];

Or use the BIT_OR() function with the GROUP BY clause:

SELECT BIT_OR(expr), group_expr1, group_expr2, ...
FROM table_name
[WHERE ...]
GROUP BY group_expr1, group_expr2, ...;

Parameters

expr

Required. A column name or expression. It accepts a value of type integer or bit.

Return value

The MySQL BIT_AND() function returns the result of performing a “bitwise OR” operation on all non-null input values, and the result is of the same type as the input parameter.

Note that the BIT_OR() function only handles non-null values. That is, null values ​​are ignored by the BIT_OR() function.

If all input values ​​are null, the function will return NULL.

BIT_OR() Examples

To demonstrate usages of the MySQL BIT_AND() function, we simulate a temporary table using the following statement and UNION and SELECT:

SELECT 4 x
UNION
SELECT 5 x
UNION
SELECT 6 x;
+---+
| x |
+---+
| 4 |
| 5 |
| 6 |
+---+
3 rows in set (0.00 sec)

The following x statement performs the BIT_OR() operation on the column:

SELECT BIT_OR(x)
FROM (
    SELECT 4 x
    UNION
    SELECT 5 x
    UNION
    SELECT 6 x
  ) t;
+-----------+
| BIT_OR(x) |
+-----------+
|         7 |
+-----------+

Here, the BIT_AND() function performs a “bitwise OR” operation on the values ​​(4, 5, 6) in the x column, the following shows how is works:

      4 -> 100
      5 -> 101
      6 -> 110
BIT_OR() = 111 = 7

So the BIT_OR() function returns 7.