从 tableB 插入 tableA where condition set case
Insert into tableA from tableB where condition set case
1st question:
我正在尝试从我有条件的表 B 插入表 A 字段并为列 A 设置大小写,但它不起作用。是否可以在没有更新的情况下进行?
insert into tableA
select date, columnA
from tableB
where tableB.date between '2015-10-01' and '2015-10-31'
set columnA = case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
;
wchiquito 回复并且有效。
2nd question:
此外,如果我还想按日期和A列分组,我想看看第一个问题的解决方案。
我能做到:
insert into tableA
select date, case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
from tableB
where tableB.date between '2015-10-01' and '2015-10-31'
group by date, case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
;
不过我也想看看有没有别的办法。避免重复编写案例的更好方法。
记住 column
是保留字,参见 9.3 Keywords and Reserved Words。
见,12.4 Control Flow Functions - CASE。
INSERT INTO `tableA`
SELECT `date`, CASE
WHEN `column` LIKE '%aaa%' THEN 'aaa'
WHEN `column` LIKE '%bbb%' THEN 'bbb'
WHEN `column` LIKE '%ccc%' THEN 'ccc'
WHEN `column` LIKE '%ddd%' THEN 'ddd'
WHEN `column` LIKE '%eee%' THEN 'eee'
ELSE `column`
END
FROM `tableB`
WHERE `tableB`.`date` BETWEEN '2015-01-01' AND '2015-01-04';
1st question:
我正在尝试从我有条件的表 B 插入表 A 字段并为列 A 设置大小写,但它不起作用。是否可以在没有更新的情况下进行?
insert into tableA
select date, columnA
from tableB
where tableB.date between '2015-10-01' and '2015-10-31'
set columnA = case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
;
wchiquito 回复并且有效。
2nd question:
此外,如果我还想按日期和A列分组,我想看看第一个问题的解决方案。
我能做到:
insert into tableA
select date, case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
from tableB
where tableB.date between '2015-10-01' and '2015-10-31'
group by date, case
when columnA like ('%aaa%') then 'aaa'
when columnA like ('%bbb%') then 'bbb'
when columnA like ('%ccc%') then 'ccc'
when columnA like ('%ddd%') then 'ddd'
when columnA like ('%eee%') then 'eee'
else columnA
end
;
不过我也想看看有没有别的办法。避免重复编写案例的更好方法。
记住 column
是保留字,参见 9.3 Keywords and Reserved Words。
见,12.4 Control Flow Functions - CASE。
INSERT INTO `tableA`
SELECT `date`, CASE
WHEN `column` LIKE '%aaa%' THEN 'aaa'
WHEN `column` LIKE '%bbb%' THEN 'bbb'
WHEN `column` LIKE '%ccc%' THEN 'ccc'
WHEN `column` LIKE '%ddd%' THEN 'ddd'
WHEN `column` LIKE '%eee%' THEN 'eee'
ELSE `column`
END
FROM `tableB`
WHERE `tableB`.`date` BETWEEN '2015-01-01' AND '2015-01-04';