Mysql data type to make null value '0', and nonnull value '1'

时间:2016-03-02 10:46:42

标签: mysql mysql-workbench

I have a table like:

column:

Emails, Country, Inactive

I want to import list "csv" to the table and I want to make it so that if a row contains data in the "inactive" column the value will be set to "1" and if its empty "0". the column "inactive" in my list contains all sorts of string and some are null. it doesn't matter what data it has it just important that after its imported to the table I’d get to know if its containing data or not assuming that if its not than the email is active.

1 个答案:

答案 0 :(得分:0)

您可以使用以下查询根据非活动字段获取值0或1:

select emails, country, if(inactive is null or inactive = '', 0, 1) as inactive
from mytable

如果我们想将数据插入到另一个表中,那么我们需要将此查询包装到insert语句中,如下所示:

insert into newtable(emails, country, inactive)
select emails, country, if(inactive is null or inactive = '', 0, 1) as inactive
from mytable

这是SQL fiddle