2013-03-11 57 views
0

出現錯誤,在我的詢問,我解決不了:子查詢列錯誤

我的查詢:

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' 

在出現的錯誤 「列弗= 3」

Msg 207, Level 16, State 1, Line 2 
Invalid column name 'lev'. 

我表列爲:

[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 

回答

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' 

雖然我甚至不明白爲什麼子查詢是必要的:

select [subject], cal 
from amir 
where textfilter like '% word %' and lev=3 AND 
    cal between '6/10/2012' AND '3/11/2013'