获取一个字符串在MySQL列中出现多少次的计数?
让我们首先创建一个表-
create table DemoTable704 (SubjectName text);
使用插入命令在表中插入一些记录-
insert into DemoTable704 values('Introduction to MySQL'); insert into DemoTable704 values('Introduction to MongoDB'); insert into DemoTable704 values('Introduction to MySQL'); insert into DemoTable704 values('Introduction to Java'); insert into DemoTable704 values('Introduction to MongoDB'); insert into DemoTable704 values('Introduction to MySQL'); insert into DemoTable704 values('Introduction to C'); insert into DemoTable704 values('Introduction to Spring and Hibernate');
使用select语句显示表中的所有记录-
select *from DemoTable704;
这将产生以下输出-
+--------------------------------------+ | SubjectName | +--------------------------------------+ | Introduction to MySQL | | Introduction to MongoDB | | Introduction to MySQL | | Introduction to Java | | Introduction to MongoDB | | Introduction to MySQL | | Introduction to C | | Introduction to Spring and Hibernate | +--------------------------------------+ 8 rows in set (0.00 sec)
以下是查询以获取字符串在列中出现的次数的计数-
SELECT COUNT(*) from DemoTable704 WHERE SubjectName LIKE '%Introduction to MySQL%';
这将产生以下输出-
+----------+ | COUNT(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)