MySQL BIT_OR is one of the Aggregate Function, which performs Bit wise OR operation on all bits. First, this function will convert all decimal values into binary values, and perform bit wise OR operation on those binary values. In this article we will show you, How to use this BIT_OR in MySQL with example.
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 function is
x | y | x BIT_OR y |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
MySQL BIT_OR Function Example 1
In this example, we are going to implement the BIT_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 shown below
The below statement will find the Bit wise 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;
OUTPUT
x = 9 = 1001
y = 12 = 1100
and the Bit wise OR for the above bits is 1101 = 8 + 4 + 0 + 1 = 13
MySQL BIT_OR – Group By Example
In this example, we are going to use the BIT_OR function along with the Group By clause. For this, We are going to use the below shown data
CODE
-- MySQL Aggregate BIT_OR Function Example USE company; SELECT Profession, BIT_OR(Yearly_Income) FROM dupcustomerdetails GROUP BY Profession;
OUTPUT
Thank You for Visiting Our Blog