通过按位运算比较值
是否可以有效地使用按位运算来比较两个值,其中第二个值是第一个值的超集?
如果属性 1、2 和 3 为 ON,我需要将其与属性 1 和 2 可以为 ON 或 OF 并且 3 必须为 ON 的值进行匹配。
enum user_attribs {
attrib1 = 0, attrib2 = 1, attrib3 = 2
}
enum spt_attribs {
attrib1 = 0, attrib2 = 1, attrib3 = 2
}
user_attribs u1 = attrib1 & attrib2 & attrib3;
spt_attribs s1 = attrib1 & attrib2 | attrib3;
使用上述代码,u1 = s1
在 SQL 中有效吗?
更新:RDBMS 是 SQL SERVER 2008 R2。我正在尝试比较数据库中存储的属性。我希望能够将“用户配置文件”与“系统配置文件”的价值进行比较,其中系统配置文件是用户配置文件的超集,例如用户配置文件应与系统配置文件匹配。如果用户位设置为1101
,它将与系统位1101
或1001
或1100
等匹配。我需要系统配置文件匹配 SQL 语句的“WHERE”子句中的两个设置。
Is it possible to use bitwise operations effectively to compare two values where the second value is a superset of the first?
If I have attributes 1, 2 and 3 as ON, I need to match that to a value where attributes 1 and 2 can be ON or OF and 3 must be ON.
enum user_attribs {
attrib1 = 0, attrib2 = 1, attrib3 = 2
}
enum spt_attribs {
attrib1 = 0, attrib2 = 1, attrib3 = 2
}
user_attribs u1 = attrib1 & attrib2 & attrib3;
spt_attribs s1 = attrib1 & attrib2 | attrib3;
Would u1 = s1
be valid in SQL with the above code?
UPDATE: The RDBMS is SQL SERVER 2008 R2. I'm trying to compare the stored attributes in the database. I'd like to be able to compare to success the value of a "user profile" to a "system profile," where the system profile is a superset of the user's, e.g. the user profile should match the system profile. If the user's bits are set to 1101
it would match the system bit 1101
or 1001
or 1100
, etc. I need the system profile to match on both settings in the "WHERE" clause of the SQL statement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还没有说明您的 RDBMS,但 SQL Server 支持按位运算符:
你会这样使用:
Oracle 有 BitAND:
MySQL 有 Bit 函数:
You haven't stated your RDBMS, but SQL Server supports bitwise operators:
And you would use like:
Oracle has BitAND:
MySQL has Bit Functions:
这不是 SQL 可以做到的事情。您可能正在谈论 TSQL 或 PL/SQL。您使用什么 RDBMS?
this is not something that you can do with SQL. You might be talking about TSQL or PL/SQL. What RDBMS are you using?