我想从系统日志中查询严重性/设施,然后将数字转换为有意义的关键字,如下所示:
select case severity
when 0 then 'emerg'
when 1 then 'Alert'
when 2 then 'Crit'
when 3 then 'Error'
when 4 then 'Warn'
when 5 then 'Notice'
when 6 then 'Info'
when 7 then 'Debug'
end,
case facility
when 0 then 'kern'
when 1 then 'user'
...
when 23 then 'local7'
end
from logs.sys_log;
而严重性范围为 0 到 7,设施范围为 0 到 23。我将得到一个很长的查询字符串。有没有更聪明的方法在MySQL中创建键>值映射,以缩短查询字符串?
使用两列severity_mapping
和facility_mapping
创建新表:
- 数
- 价值
并将数据0-emerg
等存储到第一个表,0-kern
存储到第二个表。稍后,在查询中使用 JOIN 子句。