我需要一个帮助,我在mysql中有一个带有变量flight_id的表,它看起来像:
flight_id
1R10005
1M10000000000
1M10000000001
1M10000000002
1M10000000003
1M10000000004
1M10000000005
1M10000000006
1M10000000007
1R10006
1M10000000008
1M10000000009
1M10000000010
1M10000000011
1R10007
1M10000000012
1M10000000013
1M10000000014
etc....
我想用flight_id'1R1'替换以'1M100'开头的所有flight_id
我想获得这样的表格:
1R10005
1R10005
1R10005
1R10005
1R10005
1R10005
1R10005
1R10005
1R10005
1R10006
1R10006
1R10006
1R10006
1R10006
1R10007
1R10007
1R10007
1R10007
请有人帮我解决mysql查询:)
答案 0 :(得分:2)
UPDATE myTable set flight_id=REPLACE(`flightId`, '1M100', '1R1')
WHERE `flightId` LIKE '1M100%'
答案 1 :(得分:1)
你可以试试这个:
select CASE
WHEN `fklightId` like '1M100%' THEN REPLACE(`flightID`, '1M100', '1R1') END
from your_tablename as flightId;
所以现在1M10000000001
将输出为1R100000001
如果你想删除更多的零,那么增加替换字符串中的零数
select CASE
WHEN `fklightId` like '1M100%' THEN REPLACE(`flightID`, '1M100000000', '1R1') END
from your_tablename as flightId;
将1M10000000001
作为1R101
答案 2 :(得分:1)
您需要一个ID或其他列来订购您的表格。如果您有ID,可以使用以下内容:
UPDATE
flights inner join (
select f1.id, max(f2.id) as new_id
from flights f1 left join flights f2
on f1.flight_id like '1M100%'
and f2.flight_id like '1R1%'
and f1.id>f2.id
group by f1.id) fmax
on flights.id=fmax.id
inner join flights fdesc
on fmax.new_id=fdesc.id
SET
flights.flight_id = fdesc.flight_id
对于每个航班ID,内部子查询返回new_id
,即id
从哪里获取描述。此new_id
是最大ID<比当前行,描述如1R1。