“&"是什么意思?意味着在这个 sql where 子句中? [英] What does "&" means in this sql where clause?

查看:50
本文介绍了“&"是什么意思?意味着在这个 sql where 子句中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

&"是什么意思这里的意思:

What does the "&" mean here:

select pt.TrTp, sum(pt.TrTp)
from ProdTr pt
where TransSt & 16 <> 16  // this is the row that i don´t understand..
group by pt.TrTp

推荐答案

它被称为位掩码.它用于数字中的各个位具有不同含义的情况,而不是仅表示数字本身的数字(例如,如果您将年龄保存到数据库中).

It's called a bitmask. It is used in situations where the individual bits in a number have different meanings, as opposed to a number just meaning the number itself (for instance, if you save your age to the database).

当您想象任何二进制形式的数字并想测试该数字中的某个位是否已设置时,您可以使用二进制 AND 运算符对数字和要测试的位进行测试,如下所示:

When you imagine any number in it's binary form and want to test if a certain bit in the number is set, you test it by using the binary AND operator with the number and the bit you want to test, like this:

if (number & 16 == 16)

在二进制中,这意味着以下(假设您的数字是 25):

In binary, this means the following (assuming, your number is 25):

if (00011001 & 00010000 == 00010000)

在这里您可以看到,第 5 位(从下往上计数)的数字都是 1,因此结果数字在该位有一个 1.因为没有其他的 1,所以当两个数字在这个位置都是 1 时,结果就是 16.

Here you can see, that the digits at the bit 5 (counted from the bottom up) are both 1, therefor the resulting number has a 1 at that bit. As there are no other 1s, the resulting number is 16 exactly when both numbers have a 1 at this position.

我想补充一点:将不同的含义编码到一个数据库字段中通常是不好的做法.很难通过索引进行索引和检索,甚至可能完全未编入索引,具体取决于您的 DBMS.

I would like to add: It's usually bad practice to encode different meanings into one database field. It's difficult to index and retrieve by an index, and depending on your DBMS might even be completely unindexed.

这篇关于“&"是什么意思?意味着在这个 sql where 子句中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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