sql - SQL 中的管道运算符有什么作用?

标签 sql bit-manipulation

我正在重新开发一个应用程序,发现了这个sql语句,|是什么意思?角色在这部分做(au.ExNetBits | 8) ,我以前没见过,网上也找不到答案?

SELECT au.AccountID,au.ExNetBits FROM AccountUser au (NOLOCK)<br/> WHERE au.CDAGUserId=102 and <strong>(au.ExNetBits | 8)</strong> = au.ExNetBits

最佳答案

来自the MSDN documentation ,

| (Bitwise OR) (Transact-SQL)

Performs a bitwise logical OR operation between two specified integer values as translated to binary expressions within Transact-SQL statements.

...

The bitwise | operator performs a bitwise logical OR between the two expressions, taking each corresponding bit for both expressions. The bits in the result are set to 1 if either or both bits (for the current bit being resolved) in the input expressions have a value of 1; if neither bit in the input expressions is 1, the bit in the result is set to 0.

If the left and right expressions have different integer data types (for example, the left expression is smallint and the right expression is int), the argument of the smaller data type is converted to the larger data type. In this example, the smallint expression is converted to an int.

例如,参见this fiddle ,

SELECT 1 | 1, 1 | 2, 2 | 4, 3 | 5;

输出

1    3    6    7

要解释此行为,您必须考虑操作数的位模式,

1 | 1

  00000001  = 1
| 00000001  = 1
_______________
  00000001  = 1

1 | 2

  00000001  = 1
| 00000010  = 2
_______________
  00000011  = 3

2 | 4

  00000010  = 2
| 00000100  = 4
_______________
  00000110  = 6

3 | 5

  00000011  = 3
| 00000101  = 5
_______________
  00000111  = 7

关于sql - SQL 中的管道运算符有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037429/

相关文章:

php - 无法添加或更新子行 : a foreign key constraint fails on real server

sql - 如何删除某些键值出现 'before' 的行?

c - 将 uint8_t 放在 uint64_t 中

c# - 对负整数返回零

c - C 中的移位运算符(<<、>>)是算术运算符还是逻辑运算符?

c# - 如何使用 BitShifting 将基于 int 的 IP 地址转换回字符串?

objective-c - 将 32 位结构传递给 32 位整数函数参数

mysql - SQL - IF EXISTS UPDATE ELSE INSERT 语法错误

mysql - SQL 在 select 子句中重复长公式

SQL 查询全部替换