我正在尝试更新已部分填充的表的许多行。 AREA_TITLE列具有包含州名称的区域名称,但从不在它自己的位置,因此是%%修饰符。这是我的mysql查询,不包含错误(根据Sequel Pro),但实际上不会更新表:
UPDATE localTable
SET PRIM_STATE =
CASE AREA_TITLE
WHEN '%Alabama%' THEN 'AL'
WHEN '%Alaska%' THEN 'AK'
WHEN '%Arizona%' THEN 'AZ'
WHEN '%Arkansas%' THEN 'AR'
WHEN '%California%' THEN 'CA'
WHEN '%Colorado%' THEN 'CO'
WHEN '%Connecticut%' THEN 'CT'
WHEN '%Delaware%' THEN 'DE'
WHEN '%Florida%' THEN 'FL'
WHEN '%Georgia%' THEN 'GA'
WHEN '%Hawaii%' THEN 'HI'
WHEN '%Iowa%' THEN 'IA'
WHEN '%Idaho%' THEN 'ID'
WHEN '%Illinois%' THEN 'IL'
WHEN '%Indiana%' THEN 'IN'
END
WHERE AREA_TITLE IN ('%Alabama%', '%Alaska%', '%Arizona%', '%Arkansas%', '%California%', '%Colorado%', '%Connecticut%', '%Delaware%', '%Florida%', '%Georgia%', '%Hawaii%', '%Iowa%', '%Idaho%', '%Illinois%', '%Indiana%');
非常感谢您的帮助!
样本数据:AREA_TITLE =“东北阿拉巴马州非都会区” - > PRIM_STATE ='AL' AREA_TITLE =“阿肯色州中部非都市区” - > PRIM_STATE ='AR'
答案 0 :(得分:1)
(我主要使用SQL Server,但这应该仍然适用,除非我弄错了)
CASE
语句的这种格式会将变量与每个WHEN
的提供值进行比较。您应该使用CASE
语句的the second form,其中每个WHEN
定义一个条件,并专门使用LIKE
运算符。
您的WHERE
子句也是如此,您实际上是将表中的值与文字%Alabama%
进行比较。需要LIKE
运算符,您可能需要使用多个OR
而不是IN
运算符。
这样的事情:
UPDATE localTable
SET PRIM_STATE =
CASE
WHEN AREA_TITLE LIKE '%Alabama%' THEN 'AL'
WHEN AREA_TITLE LIKE '%Alaska%' THEN 'AK'
WHEN AREA_TITLE LIKE '%Arizona%' THEN 'AZ'
WHEN AREA_TITLE LIKE '%Arkansas%' THEN 'AR'
WHEN AREA_TITLE LIKE '%California%' THEN 'CA'
WHEN AREA_TITLE LIKE '%Colorado%' THEN 'CO'
WHEN AREA_TITLE LIKE '%Connecticut%' THEN 'CT'
WHEN AREA_TITLE LIKE '%Delaware%' THEN 'DE'
WHEN AREA_TITLE LIKE '%Florida%' THEN 'FL'
WHEN AREA_TITLE LIKE '%Georgia%' THEN 'GA'
WHEN AREA_TITLE LIKE '%Hawaii%' THEN 'HI'
WHEN AREA_TITLE LIKE '%Iowa%' THEN 'IA'
WHEN AREA_TITLE LIKE '%Idaho%' THEN 'ID'
WHEN AREA_TITLE LIKE '%Illinois%' THEN 'IL'
WHEN AREA_TITLE LIKE '%Indiana%' THEN 'IN'
END
WHERE AREA_TITLE LIKE '%Alabama%' OR AREA_TITLE LIKE '%Alaska%' OR AREA_TITLE LIKE '%Arizona%' OR AREA_TITLE LIKE '%Arkansas%' OR AREA_TITLE LIKE '%California%' OR AREA_TITLE LIKE '%Colorado%' OR AREA_TITLE LIKE '%Connecticut%' OR AREA_TITLE LIKE '%Delaware%' OR AREA_TITLE LIKE '%Florida%' OR AREA_TITLE LIKE '%Georgia%' OR AREA_TITLE LIKE '%Hawaii%' OR AREA_TITLE LIKE '%Iowa%' OR AREA_TITLE LIKE '%Idaho%' OR AREA_TITLE LIKE '%Illinois%' OR AREA_TITLE LIKE '%Indiana%');
答案 1 :(得分:1)
我用sqlfiddle here
测试了它create table states
(
StateName varchar(100),
Abbreviation varchar(5)
);
create table localTable
(
PRIM_STATE varchar(100),
AREA_TITLE varchar(100)
);
insert into localtable(AREA_TITLE, PRIM_STATE) values ('Northeast Alabama Non-Metro Area','');
insert into localtable(AREA_TITLE, PRIM_STATE) values ('Central Arkansas Non-Metro Area','');
insert into states(StateName, Abbreviation) values ('Alabama','AL');
insert into states(StateName, Abbreviation) values ('Alaska','AK');
insert into states(StateName, Abbreviation) values ('Arkansas','AR');
update localtable
set localtable.PRIM_STATE = (
select states.Abbreviation
from states where
localtable.area_title LIKE CONCAT('%', states.StateName, '%')
);