使用One命令在SQL中 - 我需要将“Cars”一词的所有实例更改为“Zip-Cars”,这包括将“Cars 2”更改为“Zip-Cars 2”

时间:2015-01-29 19:06:59

标签: java mysql sql database mysql-workbench

CREATE DATABASE movies_200186807;

USE movies_200186807;

CREATE TABLE movies_200186807 (

movie_name   VARCHAR(50) NOT NULL PRIMARY KEY,

release_date DATE NOT NULL,

cost         DEC(4) NOT NULL,

revenue      DEC(4) NOT NULL
);


INSERT INTO movies_200186807 (movie_name, release_date, cost, revenue)

VALUES


('Toy Story', '1995-11-22',30 , 364),

('Toy Story 2', '1999-11-24',90 , 511),

('Toy Story 3', '2010-06-18', 200, 1070),

('A Bugs Life', '1998-11-25', 60, 363),

('Monsters Inc', ' 2001-11-02', 115, 560),

('Finding Nemo', '2003-05-30', 94, 906),

('The Incredibles', '2005-11-04', 92, 615),

('Cars', '2006-06-09', 120, 462),

('Cars 2', '2011-06-24', 200, 560),

('Ratatouille', '2007-06-29', 150, 626),

('Wall-E', '2008-06-27', 180, 533),

('Up', '2009-05-29', 175, 731),

('Brave', '2012-06-22', 185, 555),

('Monster University', '2013-06-21', 200, 744),

('Planes', '2013-08-09', 50, 220);


Here is my table I have created called movies_200186807.

I need to change all instances of the word 'Cars' to 'Zip-Cars', this includes 'Cars 2' to be changed to 'Zip-Cars 2'  




I have tried multiple times this I what I have:



--This select statements shows the 2 movies I need to change

SELECT movie_name

FROM movies_200186807

WHERE movie_name LIKE 'Cars%'; 





--Here is my query, It runs but does not change the movie_name to ''Zip Cars' AND 'Zip Cars 2'

UPDATE movies_200186807

SET movie_name = replace(movie_name, 'Zip-Cars', 'Zip-Cars 2') 

WHERE movie_name LIKE '%Cars%';

任何建议,

谢谢

2 个答案:

答案 0 :(得分:1)

'Cars' to 'Zip-Cars'替换为Zip-Cars' to 'Zip-Cars 2'

UPDATE movies_200186807
SET movie_name = replace(movie_name, 'Cars', 'Zip-Cars') 
WHERE movie_name LIKE '%Cars%';

答案 1 :(得分:0)

这会首先将Cars替换为Zip-Cars,然后将Cars 2替换为Zip-Cars 2

UPDATE movies_200186807
    SET movie_name = replace(replace(movie_name, 'Cars', 'Zip-Cars'), 'Cars 2', 'Zip-Cars 2')
    WHERE movie_name LIKE '%Cars%'