子查询列错误

yey*_*eye 0 sql subquery

我的查询中出现无法解决的错误:

我的查询:

SELECT [subject], cal
FROM (
    SELECT [subject], cal
    FROM amir
    WHERE textfilter LIKE '% word %') a 
WHERE lev=3 AND cal between '6/10/2012' AND '3/11/2013'
Run Code Online (Sandbox Code Playgroud)

错误发生在"lev = 3"

Msg 207, Level 16, State 1, Line 2
Invalid column name 'lev'.
Run Code Online (Sandbox Code Playgroud)

我的表列是:

    [RecordId] [bigint] IDENTITY(1,1) NOT NULL,
    [text] [nvarchar](max) NOT NULL,
    [textfilter] [nvarchar](max) NOT NULL,
    [mo] [int] NULL,
    [loc] [int] NULL,
    [lev] [int] NOT NULL,
    [cal] [date] NOT NULL,
 CONSTRAINT [PK_SahifehEmam] PRIMARY KEY CLUSTERED 
Run Code Online (Sandbox Code Playgroud)

Lit*_*les 5

您不在lev子查询中选择,因此在主查询中不可用.

您可以将它添加到子查询中,如下所示:

select [subject], cal 
from (
    select [subject], cal, lev
    from amir 
    where textfilter like '% word %'
) a 
where lev=3 AND cal between '6/10/2012' AND '3/11/2013'
Run Code Online (Sandbox Code Playgroud)

虽然我甚至不知道为什么子查询是必要的:

select [subject], cal 
from amir 
where textfilter like '% word %' and lev=3 AND 
    cal between '6/10/2012' AND '3/11/2013'
Run Code Online (Sandbox Code Playgroud)