什么是管道运营商在SQL呢? [英] What does the pipe operator do in SQL?

查看:85
本文介绍了什么是管道运营商在SQL呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重新开发一个应用程序,并发现这条SQL语句,什么是 | 字符在这部分做(au.ExNetBits | 8) ,我以前没有见过,不能在网上找到任何回答?

I am redeveloping an application and have found this sql statement, what does the | character do in this part (au.ExNetBits | 8), I haven't seen before and can't find any answer online?


SELECT au.AccountID,au.ExNetBits FROM AccountUser AU(NOLOCK)

WHERE au.CDAGUserId = 102和(au.ExNetBits | 8) = au.ExNetBits

SELECT au.AccountID,au.ExNetBits FROM AccountUser au (NOLOCK)
WHERE au.CDAGUserId=102 and (au.ExNetBits | 8) = au.ExNetBits

推荐答案

|在SQL中的几种方言(管道)运算符的的按位或运营商。

The | (pipe) operator in several dialects of SQL is the bitwise or operator.

在这种用法中,它被测试以确保该列的值保持施加的按位或在其上后的相同。在做同样的事情,另一种方法是使用按位与运算符(&安培; )。屏蔽只是位问题,并测试它的掩码

In this usage, it is testing to make sure that the value of the column remains the same after applying the bitwise or on it. Another approach to doing the same thing is to use the bitwise and operator (&) to mask just the bits in question and test it against the mask.

我个人觉得在和比或接近更地道,但多数民众赞成我。

I personally find the and to be more idiomatic than the or approach, but thats me.

这是SQL小提琴展示出的结果: http://sqlfiddle.com/#!6/aeb46 / 4

An SQL Fiddle demonstrating the results: http://sqlfiddle.com/#!6/aeb46/4

小提琴的内容是:

create table foo (bits integer);

insert into foo values (1), (2), (3), (4), (5), (6), (7), (8), (9)

select bits from foo where (bits | 2) = bits;
select bits from foo where (bits | 8) = bits;

select bits from foo where (bits & 2) = 2;
select bits from foo where (bits & 8) = 8;

为2返回2,3,6,7的查询,而查询为8返回8,9

The query for the 2 returns 2, 3, 6, 7 while the query for the 8 returns 8, 9.

如果你真的想要,因为如果8的位置设置测试也可以做的:

If you really wanted to, the test for if the 8's bit is set can also be done as:

select bits from foo where (bits / 8) % 2 = 1

但是,这只是就傻了点,但很有效。

But that's just a bit on the silly side, but it works.

在不使用C风格的位运算符的语言,经常会有类似的功能做按位的工作。例如,在Oracle中,应当使用 BITAND BITOR (见的 BITAND 在Oracle文档如何将用于) - 在&安培; 用Oracle SQL方言是一个参数和一个 || 的指示字符串连接。

In languages that don't use the C style bitwise operators, there is often a similar function to do the bitwise work. For example, in Oracle one would use BITAND and BITOR (see BITAND in the oracle docs for how that would be used) - the & in Oracle SQL dialect is the indication of a parameter and a || is for string concatenation.

这篇关于什么是管道运营商在SQL呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆