這樣的:有一張表number_prices: 兩個主要字段 number varchar(4) 和 price int。number字段的值從‘0000’~‘9999’,每一條記錄現在有一個規則f來確定price的值,規則是:f: if number like 'AAAA' then price = 100, else if number like 'AABB' then price = 50, else if number like 'ABBA' then price = 40,
...
2 回答

HUWWW
TA貢獻1874條經驗 獲得超12個贊
我的建議是把數據讀出來,用代碼去做匹配,然后生成這種SQL代碼。
update table_name set price=10 where id=1234;
最后把這堆SQL丟進去執行

慕神8447489
TA貢獻1780條經驗 獲得超1個贊
我優先考慮過正則表達式來解決問題,但發現mysql不支持反向引用
REGEXP:
AAAA:([0-9])\1{3}
ABBA:([0-9])([0-9])\2\1
AABB:([0-9])\1([0-9])\2
所以放棄正則后只能用最原始的if else方式,當然mysql的實現要變成case when then
UPDATE temp t SET t.`price`= CASE WHEN (SUBSTRING(t.`number`,1,1)=SUBSTRING(t.`number`,2,1) AND SUBSTRING(t.`number`,1,1)=SUBSTRING(t.`number`,3,1) AND SUBSTRING(t.`number`,1,1)=SUBSTRING(t.`number`,4,1))=TRUE THEN 100 WHEN (SUBSTRING(t.`number`,1,1)=SUBSTRING(t.`number`,2,1) ANDSUBSTRING(t.`number`,3,1)=SUBSTRING(t.`number`,4,1) AND SUBSTRING(t.`number`,2,1)<>SUBSTRING(t.`number`,3,1))=TRUE THEN 50WHEN (SUBSTRING(t.`number`,1,1)=SUBSTRING(t.`number`,4,1) ANDSUBSTRING(t.`number`,2,1)=SUBSTRING(t.`number`,3,1) AND SUBSTRING(t.`number`,1,1)<>SUBSTRING(t.`number`,2,1))=TRUE THEN 40ELSE 0 END;
添加回答
舉報
0/150
提交
取消