当我想对字段数据进行:
大于10,则变成-1
其他条件则不变
然后再进行升序排序,结果出现错误了
以下是代码:
//原来的表
+---------------------------------------+-------+
| events | total |
+---------------------------------------+-------+
| wait/io/file/innodb/innodb_data_file | 1598 |
| wait/io/file/innodb/innodb_log_file | 47 |
| wait/io/file/innodb/innodb_temp_file | 90 |
| wait/io/file/innodb/innodb_dblwr_file | 16 |
| wait/io/file/sql/binlog | 20 |
| wait/io/file/sql/binlog_index | 23 |
| wait/io/file/sql/casetest | 15 |
| wait/io/file/sql/ERRMSG | 5 |
| wait/io/file/sql/pid | 3 |
| wait/io/file/sql/misc | 5 |
| wait/io/file/sql/slow_log | 4 |
| wait/io/file/mysys/cnf | 5 |
| wait/io/file/mysys/charset | 3 |
+---------------------------------------+-------+
操作:select events, total as "pre total", case when total > 10 then -1 else total end as "new total" from x$waits_global_by_latency order by "new total";
结果:
+---------------------------------------+-----------+-----------+
| events | pre total | new total |
+---------------------------------------+-----------+-----------+
| wait/io/file/sql/binlog | 20 | -1 |
| wait/io/file/sql/binlog_index | 23 | -1 |
| wait/io/file/sql/casetest | 15 | -1 |
| wait/io/file/sql/ERRMSG | 5 | 5 |
| wait/io/file/sql/misc | 5 | 5 |
| wait/io/file/sql/pid | 3 | 3 |
| wait/io/file/sql/slow_log | 4 | 4 |
| wait/io/file/mysys/charset | 3 | 3 |
| wait/io/file/mysys/cnf | 5 | 5 |
| wait/io/file/innodb/innodb_dblwr_file | 16 | -1 |
| wait/io/file/innodb/innodb_data_file | 1580 | -1 |
| wait/io/file/innodb/innodb_log_file | 47 | -1 |
| wait/io/file/innodb/innodb_temp_file | 90 | -1 |
+---------------------------------------+-----------+-----------+
并没有按照要求来排序,不知道为什么?
sql中as后面的数据不要用引号( "" ),而是用( `` ),命名的时候不要用空格,建议更换成下划线( _ )
具体修改可以参考以下方式:推荐使用方案一
方案一:select events, total as pre_total, case when total > 10 then -1 else total end as new_total from x$waits_global_by_latency order by new_total;
方案二:select events, total as `pre total`, case when total > 10 then -1 else total end as `new total` from x$waits_global_by_latency order by `new total`;
把case语句放后面看看
select events, total as "pre total", case when total > 10 then -1 else total end as "new total" from x$waits_global_by_latency order by case when total > 10 then -1 else total end;
后面的排序改成
order by 3
我有点怀疑你是写成了下面这个样子,导致没有排序
order by 'new total'
能不能上个实际运行的截图看看?
'new total' 字段名中的空格去掉,字段名 不要用引号包围