【问题标题】:Using One command In SQL - I need to change all instances of the word 'Cars' to 'Zip-Cars', this includes 'Cars 2' to be changed to 'Zip-Cars 2'在 SQL 中使用一个命令 - 我需要将单词“Cars”的所有实例更改为“Zip-Cars”,这包括将“Cars 2”更改为“Zip-Cars 2”
【发布时间】:2015-03-29 03:28:03
【问题描述】:
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%';

任何建议,

谢谢

【问题讨论】:

    标签: java mysql sql database mysql-workbench


    【解决方案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%';
    

    【讨论】:

      【解决方案2】:

      这将首先将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%'
      

      ;

      【讨论】:

        猜你喜欢
        • 2019-01-21
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        • 2013-09-02
        • 1970-01-01
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        相关资源
        最近更新 更多