MySQL BIT_OR is one of the Aggregate Function, which performs Bitwise OR operation on all bits. First, this MySQL BIT_OR function converts all decimal values into binary values and perform bitwise OR operation on those binary values.
MySQL BIT_OR Syntax
The basic syntax of the BIT_OR Function in MySQL is as shown below:
SELECT BIT_OR (Expression) FROM [Source]
The Truth Table behind this BIT_OR function is
|x||y||x BIT_OR y|
MySQL BIT_OR Function Example 1
In this example, we are going to implement the MySQL Bitwise OR function on different columns in a table. To demonstrate the same, we are going to use the dup employee 2 table data that we have shown below
The below MySQL statement finds the Bitwise OR values of Sales 1 and Sales 2 columns.
-- MySQL BIT_OR Function Example USE company; SELECT BIT_OR(Sales1), BIT_OR(Sales2) FROM dupemploy2;
x = 9 = 1001
y = 12 = 1100
and the Bitwise OR for the above bits is 1101 = 8 + 4 + 0 + 1 = 13
MySQL BIT OR Group By Example
In this Aggregate Function example, we are going to use the BIT_OR function along with the Group By clause. For this MySQL Bitwise OR group by example, we are going to use the below shown data
-- MySQL Aggregate BIT_OR Function Example USE company; SELECT Profession, BIT_OR(Yearly_Income) FROM dupcustomerdetails GROUP BY Profession;