我想从不在数据库中的enumaration中进行选择.
例如. SELECT id FROM my_table返回值1,2,3
我想显示1 – > ‘chocolate’,2 – > ‘椰子’,3 – > ‘pizza’等SELECT CASE有效,但太复杂,难以概述许多值.我想到了类似的东西
SELECT id,array['chocolate','coconut','pizza'][id] FROM my_table
with food (fid,name) as ( values (1,'chocolate'),(2,'coconut'),(3,'pizza') ) select t.id,f.name from my_table t join food f on f.fid = t.id;
或没有CTE(但使用相同的想法):
select t.id,f.name from my_table t join ( values (1,'pizza') ) f (fid,name) on f.fid = t.id;