查找在MySQL中列值以特定子字符串结尾的行?
要查找行并用新值更新,其中列值以特定子字符串结尾,则需要使用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)
热门推荐
10 生日祝福语朋友简短的话
11 宝贝的人生祝福语简短
12 新年有哪些祝福语简短
13 简短又洋气祝福语大全
14 简短祝福语除夕快乐英文
15 生日贺卡祝福语毕业简短
16 战友聚会祝福语幽默简短
17 最新中秋祝福语简短英语
18 夏日升学宴祝福语简短