2

我收到一个错误;列名“阶段”无效。我已经尝试了所有我能想到的排列方式。

<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>

我想我最好显示整个查询。

4

3 回答 3

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

别名在哪里

于 2013-09-13T06:01:14.083 回答
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 = ...

当您想选择一个 case 表达式时,您可以定义 case 表达式,然后使用AS关键字给列别名,如上所示。

如果您想更新表中命名的列phase,那么您的表达式(如问题中提供的)将是正确的。

编辑

为了可维护性,CASE在子句中编写整个构造WHERE是相当乏味的。相反,使用内联视图(子句SELECT中的FROM语句)内的主查询并过滤phase = ....

于 2013-09-13T06:02:14.790 回答
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
于 2013-09-13T06:02:30.560 回答