WHERE 子句中的条件
我正在尝试根据表中一列的值更改 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 = 0
或 includeNullType = 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重新排列 AND 和 OR。简化:
如果 includeNull 只能是 0 或 1,这在逻辑上等价于
You can rearrange the ANDs and ORs. Simplifying:
If includeNull can only be 0 or 1, this is logically equivalent to