[英]Why SQL case isn't working
I get an error;Invalid column name 'phase'. 我收到错误;列名称“阶段”无效。 I've tried every permutation I can think of.
我已经尝试过我能想到的每一种排列。
<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>
I thought I had better show the entire query. 我以为我最好显示整个查询。
try this 尝试这个
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
EDIT: 编辑:
You need repeat whole case in where 你需要在哪里重复整个案例
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>
Or create Select(your query) where phase = condition
或者创建
Select(your query) where phase = condition
Your syntax is wrong. 你的语法错了。 This code should run:
这段代码应该运行:
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 = ...
When you want to select a case expression, you can define the case expression and then give the column alias using the AS
keyword as shown above. 如果要选择案例表达式,可以定义案例表达式,然后使用
AS
关键字给出列别名,如上所示。
If you want to update the column named phase
in a table then your expression (as provided in the question) would be the right one. 如果要更新表中名为
phase
的列,那么表达式(如问题中所提供的)将是正确的。
EDIT 编辑
For the sake of maintainability, writing the entire CASE
construct in the WHERE
clause is quite tedious. 为了可维护性,在
WHERE
子句中编写整个CASE
构造非常繁琐。 Instead, use the main query inside an inline view ( SELECT
statement in the FROM
clause) and filter on phase = ...
. 相反,在内联视图中使用主查询(
FROM
子句中的SELECT
语句)并在phase = ...
上过滤。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.