要查找行并用新值更新,其中列值以特定子字符串结尾,则需要使用LIKE运算符。
语法如下:UPDATE yourTableName
SET yourColumnName=’yourValue’
WHERE yourColumnName LIKE ‘%.yourString’;
为了理解上述语法,让我们创建一个表。创建表的查询如下:mysql> create table RowEndsWithSpecificString
-> (
-> Id int NOT NULL AUTO_INCREMENT,
-> FileName varchar(30),
-> PRIMARY KEY(Id)
-> );
现在,您可以使用insert命令在表中插入一些记录。查询如下:mysql> insert into RowEndsWithSpecificString(FileName) values('MergeSort.c');
mysql> insert into RowEndsWithSpecificString(FileName) values('BubbleSortIntroduction.pdf');
mysql> insert into RowEndsWithSpecificString(FileName) values('AllMySQLQuery.docx');
mysql> insert into RowEndsWithSpecificString(FileName) values('JavaCollections.pdf');
mysql> insert into RowEndsWithSpecificString(FileName) values('JavaServlet.pdf');
使用select语句显示表中的所有记录。查询如下:mysql> select *from RowEndsWithSpecificString;
以下是输出:+----+----------------------------+
| Id | FileName |
+----+----------------------------+
| 1 | MergeSort.c |
| 2 | BubbleSortIntroduction.pdf |
| 3 | AllMySQLQuery.docx |
| 4 | JavaCollections.pdf |
| 5 | JavaServlet.pdf |
+----+----------------------------+
5 rows in set (0.00 sec)
这是用于查找和更新列值以特定子字符串结尾的查询。以下查询将找到一个以“ .docx”结尾的子字符串,并使用一个新的“ .pdf”子字符串进行更新。查询如下:mysql> update RowEndsWithSpecificString
-> set FileName='IntroductionToCoreJava.pdf'
-> where FileName LIKE '%.docx';
Rows matched: 1 Changed: 1 Warnings: 0
现在再次检查表记录。查询如下:mysql> select *from RowEndsWithSpecificString;
以下是输出:+----+----------------------------+
| Id | FileName |
+----+----------------------------+
| 1 | IntroductionToCoreJava.pdf |
| 2 | BubbleSortIntroduction.pdf |
| 3 | IntroductionToCoreJava.pdf |
| 4 | JavaCollections.pdf |
| 5 | JavaServlet.pdf |
+----+----------------------------+
5 rows in set (0.00 sec)