如何从选择中获取值" as"

时间:2016-12-07 10:31:09

标签: mysql select reference

我有以下内容,我正试图从" id_category"中获取价值。在AS部分。我需要引用它来构建一个更远的查询链接

  case
    when  pa.category_id = 310 then 669 -- Trains
    when  pa.category_id = 309 then 2785 -- Ships
    when  pa.category_id = 311 then 631  -- Planes
  end  
      else concat("NOT FOUND FOR ",pc.name)
  end **as "id_category",**

2 个答案:

答案 0 :(得分:0)

你必须在像

这样的外部查询中获取它
select id_category, ...
from (
 select case
    when  pa.category_id = 310 then 669 -- Trains
    when  pa.category_id = 309 then 2785 -- Ships
    when  pa.category_id = 311 then 631  -- Planes
      else concat("NOT FOUND FOR ",pc.name)
  end as "id_category" from table1) xxx;

答案 1 :(得分:0)

您只能使用命令语句中的id_category。如果要在其他地方使用它,请尝试使用子查询

Select SUBQUERY.id_category
from ( select 

case
    when  pa.category_id = 310 then 669 -- Trains
    when  pa.category_id = 309 then 2785 -- Ships
    when  pa.category_id = 311 then 631  -- Planes
  end  
      else concat("NOT FOUND FOR ",pc.name)
  end **as "id_category",**
...
) AS 'SUBQUERY'
...