我有一个查询,看起来像这样:
SELECT id, description, default_error, custom_error FROM `table1`;
这给了我
(int) (Text) (Varchar) (Varchar)
id Description Default_Error custom_error
---------------------------------------------------
1 Hello Error 123
2 World Error 456 This is a custom Error
我想选择一个额外的列(“错误”),如果custom_error为EMPTY,则具有default_error的值,如果custom_error为NOT EMPTY,则具有custom_error的值。
知道如何在MySQL中执行此操作吗?
最佳答案
尝试
SELECT id, description, default_error, custom_error,
IF(custom_error='', default_error, custom_error) as error
FROM `table1`;
或-如果custom_error默认为
NULL
SELECT id, description, default_error, custom_error,
ISNULL(custom_error, default_error) as error
FROM `table1`;