我有数据库:
City:
id | name | type
1 | London | A
2 | Manchester | E
3 | Cardiff | W
我希望收到选择:
1 | London | First
2 | Manchester | Second
3 | Cardiff | Third
因此查询应将A替换为First,E替换为Second,W替换为W.
SELECT id, name, type ???? FROM City
我该怎么做?
答案 0 :(得分:2)
select id,
name,
case when type = 'A' then 'First'
when type = 'E' then 'Second'
when type = 'W' then 'Third'
end as alias_name
from City
答案 1 :(得分:1)
您应该有第三个表格,其中包含类型信息。
type | label
A | First
B | Second
C | Third
有了这些表格,您可以轻松加入所需信息。
SELECT id, name, label FROM City LEFT JOIN `type_table` USING `type`
答案 2 :(得分:0)
SELECT
id,
name,
IF(
type = 'A', 'First',
IF(
type = 'E', 'Second',
IF(
type = 'W', 'Third', type))
) as type FROM City
我进行了性能测试,在这种情况下,几个IF比WHEN快