我在查询数据时遇到麻烦.我不知道从哪里开始查询if else condition if inside if.
问题是:如何查询其类型= cpm和end_date <now()然后将结果值类型更改为之前,如果其类型= cpm和end_date> now(),则将结果值类型更改为after.我在表中有数据,结果如下:

我想得到如下结果:
Array(
[0] => Array
(
[id] => 1
[type] => free
[end_date] => 2013-06-20
)
[1] => Array
(
[id] => 4
[type] => after
[end_date] => 2013-08-29
)
[2] => Array
(
[id] => 5
[type] => before
[end_date] => 2013-06-20
)
)
Run Code Online (Sandbox Code Playgroud)
提前致谢.
这样的事情应该有效:
select
id,
case
when (type = 'cpm' and end_date < now()) then 'before'
when (type = 'cpm' and end_date > now()) then 'after'
else type
end as type,
end_date
from my_table;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6885 次 |
| 最近记录: |