通过按位运算比较值

发布于 2024-11-19 22:09:22 字数 673 浏览 2 评论 0原文

是否可以有效地使用按位运算来比较两个值,其中第二个值是第一个值的超集?

如果属性 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,它将与系统位110110011100等匹配。我需要系统配置文件匹配 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听风吹 2024-11-26 22:09:22

您还没有说明您的 RDBMS,但 SQL Server 支持按位运算符:

你会这样使用:

-- must be powers of 2...
attrib1 = 1
attrib2 = 2
attrib3 = 4
...

-- Check if attrib1 and atrrib2 are set
IF ((col1 & (atrib1 | attrib2)) = (atrib1 | attrib2)

Oracle 有 BitAND:

MySQL 有 Bit 函数:

You haven't stated your RDBMS, but SQL Server supports bitwise operators:

And you would use like:

-- must be powers of 2...
attrib1 = 1
attrib2 = 2
attrib3 = 4
...

-- Check if attrib1 and atrrib2 are set
IF ((col1 & (atrib1 | attrib2)) = (atrib1 | attrib2)

Oracle has BitAND:

MySQL has Bit Functions:

不必你懂 2024-11-26 22:09:22

这不是 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文