我正在尝试从字段AS“initial_cost”中选择一个值,但是该值位于不同的列中,具体取决于channel_id字段的值,如果channel_id为1,则它在field_id_4中,如果它是2则则为; s在field_id_10。
我尝试了下面的各种突变,但无济于事,
SELECT (CASE WHEN channel_id = 1
THEN field_id_4
FROM exp_channel_data
WHEN channel_id = 2
THEN field_id_10 FROM exp_channel_data
END) as initial_cost
有人可以帮忙吗?
答案 0 :(得分:6)
喜欢这个吗?
select title
,entry_date
,case when channel_id = 1 then field_id_4
when channel_id = 2 then field_id_10
end as initial_cost
from exp_channel_data;
答案 1 :(得分:2)
或使用case <column> when <value>
代替case when <condition>
:
select case channel_id
when 1 then field_id_4
when 2 then field_id_10
end as initial_cost
from exp_channel_data
答案 2 :(得分:1)
SELECT CASE WHEN channel_id = 1 THEN field_id_4
WHEN channel_id = 2 THEN field_id_10 END as intialcost
FROM exp_channel_data