在SQL中使用LIKE

时间:2017-11-28 21:06:33

标签: sql-server sql-like

我用来创建大小类报告的第一部分代码。它工作得很漂亮。但是,我需要缩小此操作的重点,但仅限于以11或21开头的CODE值。如果有帮助,则代码是字符(6)主键字段。我试图使用的Where语句位于底部。我把它放在第一个SELECT语句之后。我的语法错误不正确。

在哪里放置where语句是否重要? 其次,where语句对于它的用途是否正确?

SELECT 
[Areas].[Area], 
[classSizes].[sizeclass],
SUM([data].[Employment]) AS [Employment in Size Class],
CASE COUNT([data].[Employment])
    WHEN 0 THEN NULL
    ELSE COUNT([data].[Employment])
    END  AS [Number of Sites]
    FROM (SELECT DISTINCT 
    [Area] 
FROM [sizeclassreport]) AS [Areas] 
CROSS JOIN (SELECT DISTINCT 
            [sizeclass] 
        FROM [sizeclassreport]) AS [classSizes]
 LEFT OUTER JOIN [sizeclassreport] [data]
 ON [Areas].[Area] = [data].[Area] 
    AND [classSizes].[sizeclass] = [data].[sizeclass]
GROUP BY
[areas].[Area], 
[classSizes].[sizeclass]
ORDER BY
[areas].[Area], 
[classSizes].[sizeclass]

Where code like '11%' or '21%'

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'Where'

1 个答案:

答案 0 :(得分:2)

除了其他人对条款顺序的说法之外,这是无效的语法:

Where code like '11%' or '21%'

相反,你需要写:

Where code like '11%' or code like '21%'