清理我的条件WHERE子句

时间:2013-09-15 15:50:17

标签: sql sql-server-2008 conditional where

这段代码有效,我只是想学习如何清理它,所以它不会跨越很多行,而且where语句只是在几行上很好和整洁:

SELECT
    case_id, service_type_id 
FROM 
    cases_service_types
WHERE
    dbo.cases_service_types.service_type_id = '3'
    OR dbo.cases_service_types.service_type_id = '4'
    OR dbo.cases_service_types.service_type_id = '5'
    OR dbo.cases_service_types.service_type_id = '6'
    OR dbo.cases_service_types.service_type_id = '8'
    OR dbo.cases_service_types.service_type_id = '9'

2 个答案:

答案 0 :(得分:2)

您正在寻找IN

SELECT case_id, 
       service_type_id 
FROM   cases_service_types 
WHERE  dbo.cases_service_types.service_type_id IN ( 3, 4, 5, 6, 8, 9 ) 

答案 1 :(得分:2)

SELECT
    case_id, 
    service_type_id 
FROM 
    dbo.cases_service_types
WHERE
     service_type_id IN ('3', '4', '5', '6', '8', '9' )

如果你不小心错过了'7',那就更好了:

SELECT
    case_id, 
    service_type_id 
FROM 
    dbo.cases_service_types
WHERE
     service_type_id BETWEEN '3' AND '9'