Вопрос

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

Alias in where

Другие советы

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.

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.

EDIT

For the sake of maintainability, writing the entire CASE construct in the WHERE clause is quite tedious. Instead, use the main query inside an inline view (SELECT statement in the FROM clause) and filter on 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 с атрибуция
Не связан с StackOverflow
scroll top