我收到错误;列名称“阶段”无效。我已经尝试了我能想到的每一种排列。
<cfargument name="locationFilter" default="" />
<cfargument name="educationFilter" default="" />
<cfargument name="workFilter" default="" />
<cfargument name="diversityFilter" default="" />
<cfargument name="phaseFilter" default="" />
<cfquery name="QMentors" datasource="#request.dsn_live#">
SELECT
(case
when datepart(year,getdate())-cast(yearstarted as integer) > 29 then '5'
when datepart(year,getdate())-cast(yearstarted as integer) > 13 then '4'
when datepart(year,getdate())-cast(yearstarted as integer) > 5 then '3'
when datepart(year,getdate())-cast(yearstarted as integer) > 1 then '2'
else '1'
end) as phase, *
FROM mentors
WHERE 0=0
AND mentortype='mentor'
AND approved='true'
AND active='true'
AND mentorcat LIKE '%general%'
<cfif arguments.locationFilter neq ""> AND location LIKE <cfqueryparam value="%#arguments.locationFilter#%" /></cfif>
<cfif arguments.educationFilter neq ""> AND educationhistory LIKE <cfqueryparam value="%#arguments.educationFilter#%" /></cfif>
<cfif arguments.workFilter neq ""> AND workhistory LIKE <cfqueryparam value="%#arguments.workFilter#%" /></cfif>
<cfif arguments.diversityFilter eq "Diversity"> AND mentorcat LIKE <cfqueryparam value="%#arguments.diversityFilter#%" /></cfif>
<cfif arguments.phaseFilter neq ""> AND phase = <cfqueryparam value="#arguments.phaseFilter#" /></cfif>
ORDER BY lastname
</cfquery>
我以为我最好显示整个查询。
答案 0 :(得分:5)
试试这个
SELECT
(case
when datepart(year,getdate())-cast(yearstarted as integer) > 29 then '5'
when datepart(year,getdate())-cast(yearstarted as integer) > 13 then '4'
when datepart(year,getdate())-cast(yearstarted as integer) > 5 then '3'
when datepart(year,getdate())-cast(yearstarted as integer) > 1 then '2'
else '1'
end) as phase, *
FROM table
编辑:
你需要重复整个案例
AND case
when datepart(year,getdate())-cast(yearstarted as integer) > 29 then '5'
when datepart(year,getdate())-cast(yearstarted as integer) > 13 then '4'
when datepart(year,getdate())-cast(yearstarted as integer) > 5 then '3'
when datepart(year,getdate())-cast(yearstarted as integer) > 1 then '2'
else '1'
end) = <cfqueryparam value="#arguments.phaseFilter#" /></cfif>
或创建Select(your query) where phase = condition
答案 1 :(得分:3)
你的语法错了。这段代码应该运行:
SELECT *
FROM (SELECT case
when datepart(year,getdate())-cast(yearstarted as integer) > 29 then '5'
when datepart(year,getdate())-cast(yearstarted as integer) > 13 then '4'
when datepart(year,getdate())-cast(yearstarted as integer) > 5 then '3'
when datepart(year,getdate())-cast(yearstarted as integer) > 1 then '2'
else '1'
end AS phase,
col1, col2, col3, ... -- The list of columns that you want to select
FROM table)
WHERE plase = ...
如果要选择案例表达式,可以定义案例表达式,然后使用AS
关键字给出列别名,如上所示。
如果要更新表中名为phase
的列,那么您的表达式(如问题中所提供的)将是正确的。
修改强>
为了可维护性,在CASE
子句中编写整个WHERE
构造非常繁琐。相反,在内联视图(SELECT
子句中的FROM
语句)中使用主查询,并在phase = ...
上过滤。
答案 2 :(得分:0)
SELECT
case
when datepart(year,getdate())-cast(yearstarted as integer) > 29 then '5'
when datepart(year,getdate())-cast(yearstarted as integer) > 13 then '4'
when datepart(year,getdate())-cast(yearstarted as integer) > 5 then '3'
when datepart(year,getdate())-cast(yearstarted as integer) > 1 then '2'
else '1'
end as phase,
*
FROM table