2010-12-04 54 views
0

我有一个查询,看起来像这样:有,如果custom_error的default_error的价值MySQL:如何从另一列中选择一个列的值,具体取决于哪些列是空的?

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 

我想选择一个额外的列(“错误”)是EMPTY,如果custom_error不是EMPTY,则为custom_error的值。

任何想法如何在MySQL中做到这一点?

回答

1

尝试

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`; 
2

如果custom_error是空空的时候比你可以使用这个:

select id, description, coalesce(custom_error, default_error) 
0
SELECT id, description, default_error, custom_error, 
    IF(custom_error='', default_error, custom_error) as error 
FROM `table1`; 
相关问题