WHERE 子句中的条件

发布于 2025-01-06 06:49:29 字数 1318 浏览 3 评论 0原文

我正在尝试根据表中一列的值更改 WHERE 子句。 我的表格是:

server_updates

k1ID        updID
----------- -----------

server_updates_cat

sucID        updID       catID       selectChildren   includeNullType
-----------  ----------- ----------- ---------------- --------------

server_updates_types

sucID       typeID
----------- -----------

这就是我正在做的事情:

DECLARE @k1_catID INT
DECLARE @k4_type INT

SET @k1_catID = 30
SET @k4_type = 1

SELECT suc.sucID, suc.updID FROM server_updates su
INNER JOIN server_updates_cat suc ON suc.updID = su.updID

WHERE
includeNullType = 0
    AND suc.catID = @k1_catID
    AND selectChildren = 1
    AND @k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)
OR
includeNullType = 1 
    AND KSUC.catID = @k1_catID
    AND selectChildren = 1
    AND (@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID) OR @k4_type IS NULL)

我也不想重复所有条件includeNullType = 0includeNullType = 1,但更改我真正需要更改的唯一内容,即:

@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)

还有另一种方法吗?

I'm trying to change my WHERE clause based on the value of one column of my table.
My tables are:

server_updates

k1ID        updID
----------- -----------

server_updates_cat

sucID        updID       catID       selectChildren   includeNullType
-----------  ----------- ----------- ---------------- --------------

server_updates_types

sucID       typeID
----------- -----------

And here's what I'm doing:

DECLARE @k1_catID INT
DECLARE @k4_type INT

SET @k1_catID = 30
SET @k4_type = 1

SELECT suc.sucID, suc.updID FROM server_updates su
INNER JOIN server_updates_cat suc ON suc.updID = su.updID

WHERE
includeNullType = 0
    AND suc.catID = @k1_catID
    AND selectChildren = 1
    AND @k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)
OR
includeNullType = 1 
    AND KSUC.catID = @k1_catID
    AND selectChildren = 1
    AND (@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID) OR @k4_type IS NULL)

I would like to not have to repeat all conditions either includeNullType = 0 or includeNullType = 1, but change the only thing I really need to change, which is:

@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID)

Is there another way of doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

寂寞花火° 2025-01-13 06:49:29
WHERE suc.catID = @k1_catID
AND selectChildren = 1
AND (
(includeNullType = 0   
AND @k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID))
OR
(includeNullType = 1 
    AND (@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID) OR @k4_type IS NULL))
)
WHERE suc.catID = @k1_catID
AND selectChildren = 1
AND (
(includeNullType = 0   
AND @k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID))
OR
(includeNullType = 1 
    AND (@k4_type IN (SELECT typeID FROM server_updates_types sut WHERE sut.sucID = suc.sucID) OR @k4_type IS NULL))
)
薯片软お妹 2025-01-13 06:49:29

您可以重新排列 AND 和 OR。简化:

includeNull = 0 AND LONG_CONDITION
OR
includeNull = 1 AND (LONG_CONDITION OR COND2)

如果 includeNull 只能是 0 或 1,这在逻辑上等价于

LONG_CONDITION OR (includeNull = 1 AND COND2)

You can rearrange the ANDs and ORs. Simplifying:

includeNull = 0 AND LONG_CONDITION
OR
includeNull = 1 AND (LONG_CONDITION OR COND2)

If includeNull can only be 0 or 1, this is logically equivalent to

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