我在SQL查询中有一个很大的选择案例。
(select case tb_usuario.int_id_cargo
when 13
then '20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53'
when 20
then ''
when 21
then '20'
when 22
then ''
when 23
then '22'
when 24
then '20,21,22,23'
when 25
then '20,21,22,23,24'
when 26
then '20,21,22,23,24,25'
when 27
then ''
when 28
then '27'
when 29
then ''
when 30
then '29'
when 31
then '27,28'
when 32
then '27,28,31'
when 33
then '29,30'
when 34
then '27,28,29,30,31,32,33'
when 35
then ''
else ''
end) as subs
如果你注意你会发现有很多情况会返回一个空字符串。我想检查那些在一个案例中返回空字符串的情况,即:
(select case tb_usuario.int_id_cargo
when 20,22,27,29,35
then ''
[...]
这可能吗?
答案 0 :(得分:3)
使用表(临时表)如何在不使用CASE
语句的情况下选择此值?
migration (table name)
old_id new_id
--------- --------------
24 20,21,22,23
30 29
.....
,您的查询将如下所示
select migration.new_id
from migration inner join tb_usuario
on migration.old_id = tb_usuario.int_id_cargo
编辑:对于使用空白字符串指向new_id的多个old_id,您必须单独创建条目
例如(假设old_id 1& 2指向new_id - >空白)
migration (table name)
old_id new_id
--------- --------------
1
2
这样做的好处是可以使用此表将new_id从空白更改为其他内容,而不是更改上面写的查询。
答案 1 :(得分:2)
我会把它留给ELSE
条款。仅在CASE
语句中为您要匹配的ID命名。除非您有理由将这些ID保留在案例陈述中。
(select case tb_usuario.int_id_cargo
when 13
then '20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53'
when 21
then '20'
when 23
then '22'
when 24
then '20,21,22,23'
when 25
then '20,21,22,23,24'
when 26
then '20,21,22,23,24,25'
when 28
then '27'
when 30
then '29'
when 31
then '27,28'
when 32
then '27,28,31'
when 33
then '29,30'
when 34
then '27,28,29,30,31,32,33'
else ''
end) as subs
答案 2 :(得分:1)
使用:
(SELECT CASE
WHEN tb_usuario.int_id_cargo = 13 THEN
'20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53'
WHEN tb_usuario.int_id_cargo IN(20, 22, 27, 29, 35) THEN
''
WHEN tb_usuario.int_id_cargo = 21 THEN
'20'
WHEN tb_usuario.int_id_cargo = 23 THEN
'22'
WHEN tb_usuario.int_id_cargo = 24 THEN
'20,21,22,23'
WHEN tb_usuario.int_id_cargo = 25 THEN
'20,21,22,23,24'
WHEN tb_usuario.int_id_cargo = 26 THEN
'20,21,22,23,24,25'
WHEN tb_usuario.int_id_cargo = 28 THEN
'27'
WHEN tb_usuario.int_id_cargo = 30 THEN
'29'
WHEN tb_usuario.int_id_cargo = 31 THEN
'27,28'
WHEN tb_usuario.int_id_cargo = 32 THEN
'27,28,31'
WHEN tb_usuario.int_id_cargo = 33 THEN
'29,30'
WHEN tb_usuario.int_id_cargo = 34 THEN
'27,28,29,30,31,32,33'
ELSE ''
END) as subs
虽然我没有看到区分应该返回零长度字符串的特定值与仅让ELSE(返回相同值)的特性值有关:
(SELECT CASE tb_usuario.int_id_cargo
WHEN 13 THEN
'20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53'
WHEN 21 THEN
'20'
WHEN 23 THEN
'22'
WHEN 24 THEN
'20,21,22,23'
WHEN 25 THEN
'20,21,22,23,24'
WHEN 26 THEN
'20,21,22,23,24,25'
WHEN 28 THEN
'27'
WHEN 30 THEN
'29'
WHEN 31 THEN
'27,28'
WHEN 32 THEN
'27,28,31'
WHEN 33 THEN
'29,30'
WHEN 34 THEN
'27,28,29,30,31,32,33'
ELSE ''
END) as subs
答案 3 :(得分:0)
select (case
when tbl_usuario.int_id_cargo in (20,22,27,29,35,.....) then ''
else
case tbl_usuario.int_id_cargo
when 23 then '22'
when 25 then '20,21,22,23,24'
when ....
end
end) as subs
用其余的id替换.... 然后修复第二个case语句,以匹配所有其他值