MySQL中 CASE WHEN THEN then后面跟常量是没问题的 如果改成字段就没值
的有关信息介绍如下:可以的吧,出NULL是不是因为你字段的内容就是NULL啊。
这里是一个测试,可以看到使用列名value来做then的值是可以的:
mysql>
select
*
from
aaa;
+------+-------+
|
id
|
value
|
+------+-------+
|
1
|
1
|
|
2
|
3
|
|
3
|
4
|
|
4
|
4
|
|
5
|
4
|
|
6
|
6
|
|
8
|
8
|
|
7
|
7
|
|
9
|
8
|
|
10
|
10
|
|
11
|
11
|
|
12
|
11
|
|
13
|
11
|
|
14
|
11
|
|
15
|
12
|
+------+-------+
15
rows
in
set
(0.00
sec)
mysql>
select
id,
value,
case
id
when
1
then
value
when
2
then
2
*
value
when
3
then
'xxx'
else
end
as
case_col
->
from
aaa;
+------+-------+----------+
|
id
|
value
|
case_col
|
+------+-------+----------+
|
1
|
1
|
1
|
|
2
|
3
|
6
|
|
3
|
4
|
xxx
|
|
4
|
4
|
|
|
5
|
4
|
|
|
6
|
6
|
|
|
8
|
8
|
|
|
7
|
7
|
|
|
9
|
8
|
|
|
10
|
10
|
|
|
11
|
11
|
|
|
12
|
11
|
|
|
13
|
11
|
|
|
14
|
11
|
|
|
15
|
12
|
|
+------+-------+----------+
15
rows
in
set
(0.00
sec)