SELECT Ad.Id, Newspaper,
(select Organization from JobOrganization where JobOrganization.Id = Ad.OrganizationId) as Organization,
Ad.PublishDate, Ad.LastDate,Ad.Url, Job.Id as JobId,
(select JobTitle from JobTitle where JobTitle.Id = Job.TitleId) as JobTitle1,
QualificationId, ExpInYears, CategoryId
FROM Ad inner join Job on Ad.Id = Job.AdId
Where JobTitle1 Like @title or @title is null
Order by
case When @sortCol='PublishDate' and @sortDir='ASC' Then Ad.PublishDate End ASC,
case When @sortCol='PublishDate' and @sortDir='DESC' Then Ad.PublishDate End DESC,
case When @sortCol='LastDate' and @sortDir='ASC' Then Ad.LastDate End ASC,
case When @sortCol='LastDate' and @sortDir='DESC' Then Ad.LastDate End DESC
错误:列名'JobTitle1'无效。
我正在使用SQL-2008
答案 0 :(得分:3)
我担心你不能在where子句中使用别名,
SELECT Ad.Id, Newspaper,
(select Organization from JobOrganization where JobOrganization.Id = Ad.OrganizationId) as Organization,
Ad.PublishDate, Ad.LastDate,Ad.Url, Job.Id as JobId,
(select JobTitle from JobTitle where JobTitle.Id = Job.TitleId) as JobTitle1,
QualificationId, ExpInYears, CategoryId
FROM Ad inner join Job on Ad.Id = Job.AdId
Where (select JobTitle from JobTitle where JobTitle.Id = Job.TitleId)Like @title or @title is null
Order by
case When @sortCol='PublishDate' and @sortDir='ASC' Then Ad.PublishDate End ASC,
case When @sortCol='PublishDate' and @sortDir='DESC' Then Ad.PublishDate End DESC,
case When @sortCol='LastDate' and @sortDir='ASC' Then Ad.LastDate End ASC,
case When @sortCol='LastDate' and @sortDir='DESC' Then Ad.LastDate End DESC
所以在那种情况下我正在使用View ..
答案 1 :(得分:1)
此处缺少JobTitle1引用名称范围(其中 JobTitle1 像@title或@title一样为null),
试试这个( Where Ad.JobTitle1 Like @title or @title is null )
答案 2 :(得分:0)
您不能在WHERE子句中使用别名,因为WHERE子句是在SELECT子句之前计算的。