如何使用 case when 语句在列中设置 Mysql 加载的 table 项目
How can I set Mysql loaded table items in a column using a case when statement
我所在的csv文件中的一列有3个字母:c、e和n。当我导入数据时,我希望它们分别变为 2、1 和 0。当我在SET语句中使用运行 CASE WHEN时,table每行只有returns 2个。我在这里做错了什么?
load data local infile 'C:\Scripts\Storagefile.csv'
into table infra.storage
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 rows
(status, gasday, gasinstorage, percent_full, net_change, injection, withdrawal, workingstorage, injection_cap, withdrawal_cap, @dummy)
set
status = (SELECT case
when status = 'C' then 2
when status = 'E' then 1
when status = 'N' then 0 end),
entity = 'EU',
gasinstorage = gasinstorage * 3.142142,
net_change = net_change * 3.142142,
injection = injection * 3.142142,
withdrawal = withdrawal * 3.142142,
workingstorage = workingstorage * 3.142142,
injection_cap = injection_cap * 3.142142,
withdrawal_cap = withdrawal_cap * 3.142142,
reportdate = current_timestamp()
这里有一个 sql 的解决方案。要preprocess the input,需要先将CSV值赋给一个变量,然后使用set
子句。
这应该接近你想要的:
load data local infile 'C:\Scripts\Storagefile.csv'
into table infra.storage (
@status,
gasday,
@gasinstorage,
percent_full,
@net_change,
@injection,
@withdrawal,
@workingstorage,
@injection_cap,
@withdrawal_cap,
@dummy
)
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 rows
set
status = case @status
when 'C' then 2
when 'E' then 1
when 'N' then 0
end,
entity = 'EU',
gasinstorage = @gasinstorage * 3.142142,
net_change = @net_change * 3.142142,
injection = @injection * 3.142142,
withdrawal = @withdrawal * 3.142142,
workingstorage = @workingstorage * 3.142142,
injection_cap = @injection_cap * 3.142142,
withdrawal_cap = @withdrawal_cap * 3.142142,
reportdate = current_timestamp()
;
我所在的csv文件中的一列有3个字母:c、e和n。当我导入数据时,我希望它们分别变为 2、1 和 0。当我在SET语句中使用运行 CASE WHEN时,table每行只有returns 2个。我在这里做错了什么?
load data local infile 'C:\Scripts\Storagefile.csv'
into table infra.storage
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 rows
(status, gasday, gasinstorage, percent_full, net_change, injection, withdrawal, workingstorage, injection_cap, withdrawal_cap, @dummy)
set
status = (SELECT case
when status = 'C' then 2
when status = 'E' then 1
when status = 'N' then 0 end),
entity = 'EU',
gasinstorage = gasinstorage * 3.142142,
net_change = net_change * 3.142142,
injection = injection * 3.142142,
withdrawal = withdrawal * 3.142142,
workingstorage = workingstorage * 3.142142,
injection_cap = injection_cap * 3.142142,
withdrawal_cap = withdrawal_cap * 3.142142,
reportdate = current_timestamp()
这里有一个 sql 的解决方案。要preprocess the input,需要先将CSV值赋给一个变量,然后使用set
子句。
这应该接近你想要的:
load data local infile 'C:\Scripts\Storagefile.csv'
into table infra.storage (
@status,
gasday,
@gasinstorage,
percent_full,
@net_change,
@injection,
@withdrawal,
@workingstorage,
@injection_cap,
@withdrawal_cap,
@dummy
)
fields terminated by ','
enclosed by '"'
lines terminated by '\r\n'
ignore 1 rows
set
status = case @status
when 'C' then 2
when 'E' then 1
when 'N' then 0
end,
entity = 'EU',
gasinstorage = @gasinstorage * 3.142142,
net_change = @net_change * 3.142142,
injection = @injection * 3.142142,
withdrawal = @withdrawal * 3.142142,
workingstorage = @workingstorage * 3.142142,
injection_cap = @injection_cap * 3.142142,
withdrawal_cap = @withdrawal_cap * 3.142142,
reportdate = current_timestamp()
;