📜  MySQL 中的 BIT_OR()函数

📅  最后修改于: 2022-05-13 01:55:40.164000             🧑  作者: Mango

MySQL 中的 BIT_OR()函数

MySQL 中的BIT_OR()函数用于返回给定表达式中所有位的按位或。它首先将所有十进制值转换为二进制值,然后对这些二进制值进行按位或运算。

句法 :

BIT_OR(expr)

参数:此方法只接受一个参数。

  • 表达式 –
    我们要应用 BIT_OR函数的输入表达式。

返回:它返回给定表达式中所有位的按位或。

示例-1:
为了演示 BIT_OR函数的工作,我们必须首先创建一个名为 EmployeeDetails 的表。

CREATE TABLE EmployeeDetails(
Employee_Id INT AUTO_INCREMENT,  
Employee_Name VARCHAR(100) NOT NULL,
Working_At VARCHAR(20) NOT NULL,
Work_Location  VARCHAR(20) NOT NULL,
Joining_Date DATE NOT NULL,
Annual_Income INT  NOT NULL,
PRIMARY KEY(Employee_Id )
);

现在,将值插入表中 -

INSERT INTO EmployeeDetails (Employee_Name, Working_At, 
Work_Location, Joining_Date, Annual_Income)
VALUES
('Amit Khan', 'XYZ Digital', 'Kolkata', '2019-10-06', 350000),
('Shreetama Pal', 'ABC Corp.', 'Kolkata', '2018-12-16', 500000),
('Aniket Sharma', 'PQR Soln.', 'Delhi', '2020-01-11', 300000),
('Maitree Jana', 'XYZ Digital', 'Kolkata', '2019-05-01', 400000),
('Priyanka Ojha', 'ABC Corp.', 'Delhi', '2019-02-13', 350000),
('Sayani Mitra', 'XYZ Digital', 'Kolkata', '2019-09-15', 320000),
('Nitin Dey', 'PQR Soln.', 'Delhi', '2019-10-06', 250000),
('Sujata Samanta', 'PQR Soln.', 'Kolkata', '2020-10-06', 350000),
('Sudip Majhi', 'ABC Corp.', 'Delhi', '2018-10-30', 600000),
('Sanjoy Kohli', 'XYZ Digital', 'Delhi', '2019-04-18', 450000);

要验证使用以下命令如下 -

Select * FROM EmployeeDetails;

输出 :

EMPLOYEE_IDEMPLOYEE_NAMEWORKING_ATWORK_LOCATIONJOINING_DATEANNUAL_INCOME
1Amit KhanXYZ DigitalKolkata2019-10-06350000
2Shreetama PalABC Corp.Kolkata2018-12-16500000
3Aniket SharmaPQR Soln.Delhi2020-01-11300000
4Maitree JanaXYZ DigitalKolkata2019-05-01400000
5Priyanka OjhaABC Corp.Delhi2019-02-13350000
6Sayani MitraXYZ DigitalKolkata2019-09-15320000
7Nitin DeyPQR Soln.Delhi2019-10-06250000
8Sujata SamantaPQR Soln.Kolkata2020-10-06350000
9Sudip MajhiABC Corp.Delhi2018-10-30600000
10Sanjoy KohliXYZ DigitalDelhi2019-04-18450000

现在我们将从 EmployeeDetails 表中找到公司所有员工年收入的 BIT_OR。

SELECT Working_At, BIT_OR(Annual_Income) AS BITORINCOME 
FROM EmployeeDetails group by Working_At;

输出 :

WORKING_ATBITORINCOME
XYZ Digital524272
ABC Corp.1046512
PQR Soln.514032

示例 2 :
现在我们将从 EmployeeDetails 表中找到所有员工地点的年收入的 BIT_OR。

SELECT Work_Location, BIT_OR(Annual_Income) AS BITORINCOME 
FROM EmployeeDetails 
Group By Work_Location;

输出 :

WORK_LOCATIONBITORINCOME
Kolkata524208
Delhi1048560

示例 3 :
首先创建一个 StudentMarks 表 -

CREATE TABLE StudentMarks(
StudentId INT AUTO_INCREMENT,  
StudentName VARCHAR(100) NOT NULL,
Class VARCHAR(20) NOT NULL,
Roll INT NOT NULL,
Sub1Mark INT NOT NULL,
Sub2Mark INT NOT NULL,
Sub3Mark INT NOT NULL,
TotalMarks INT NOT NULL,
PRIMARY KEY(StudentId)
);

向表中插入数据——

INSERT INTO StudentMarks
(StudentName, Class, Roll, Sub1Mark, 
Sub2Mark, Sub3Mark, TotalMarks)
VALUES
('Amit Jana', 'V', 10100, 85, 80, 95, 260),
('Labanya Mallick', 'VI', 11000, 81, 89, 95, 265),
('Virat Sharma', 'VI', 12000, 75, 83, 90, 248),
('Sayani Samanta', 'V', 13000, 95, 90, 99, 284),
('Riyanka Panda', 'V', 14000, 70, 87, 88, 245),  
('Ritika Shah', 'VI', 15000, 78, 89, 90, 257);

要验证使用以下命令如下 -

SELECT  * From StudentMarks;

输出 :

STUDENTIDSTUDENTNAMECLASSROLLSUB1MARKSUB2MARKSUB3MARKTOTALMARKS
1Amit JanaV10100858095260
2Labanya MallickVI11000818995265
3Virat SharmaVI12000758390248
4Sayani SamantaV13000959099284
5Riyanka PandaV14000708788245
6Ritika ShahVI15000788990257

现在我们将从 StudentMarks 表中找到所有学生班级的总分的 BIT_OR。

SELECT Class, BIT_OR(TotalMarks) AS BITORMARKS 
FROM StudentMarks 
Group By Class;

输出 :

CLASSBITORMARKS
V509
VI505