我试图解决的问题:
返回"前缀"的值遵循以下条件:
model_hyouji
是一个数字,范围从0到9,请设置prefix = 0
model_hyouji
是一个字母,范围从a到z或A到Z,请设置prefix = 1
prefix = 2
我使用了这个SQL:
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case prefix
when 'a' then 1
when 'b' then 2
else 3
end
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
我收到此错误:
未知栏'
prefix
'在'field list
'
答案 0 :(得分:1)
假设你的列model_hyouji是varchar(1)(否则使用substr(model_hyouji,1))
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji between '0' and '9' then 0
when model_hyouji between 'a' and 'z' then 1
when model_hyouji between 'A' and 'Z' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
或者您可以使用regexp
SELECT model_code,
model_hyouji,
model_name_prefix,
model_kana_prefix,
model_count,
case
when model_hyouji REGEXP '^[0-9]{1}$' then 0
when model_hyouji REGEXP '^[A-Za-z]$' then 1
else 2
end prefix
FROM webikept.mst_model_v2
where model_maker_code = 1
and model_displacement between 51 and 125
and (SELECT substring(model_hyouji,1,1) as prefix
FROM webikept.mst_model_v2 )
答案 1 :(得分:0)
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
when prefix = 'a' then 1 when 'b' then 2 else 3 end
前缀=
时需要CASE但您也没有指定要使用的前缀列,因此未知列上的错误
E.G
SELECT
model_code,model_hyouji,model_name_prefix,model_kana_prefix,model_count,
case
WHEN model_kana_prefix = 'a' then 1
WHEN model_kana_prefix = 'b' then 2
else 3 end