一个字段内的 MySQL 查询出现次数

标签 mysql sql

我在名为 b_orders 的表中有一个名为 invoice_notes 的字段。在此字段中,“信用卡被拒绝”字样将出现多次,具体取决于信用卡被拒绝的次数。我正在尝试编写一个查询以仅选择短语“Credit Card Declined”出现少于 4 次的记录?

是这样的: SELECT * FROM b_orders where invoice_notes has "Credit Card Declined"< 4 "

感谢任何帮助。 谢谢

最佳答案

这听起来像是答案:http://pisceansheart.wordpress.com/2008/04/15/count-occurrence-of-character-in-a-string-using-mysql/
引用:

1. Counting the number of characters in the original string
2. Temporarily ‘deleting’ the character you want to count and count the string again
3. Subtract the first count from the second to get the number of occurrences

    SELECT LENGTH('foobarfoobarfoobar') - LENGTH(REPLACE('foobarfoobarfoobar', 'b', '')) AS occurrences  

对于您的具体示例:

SELECT * FROM b_orders where (length(invoice_notes) - length(replace(invoice_notes, 'Credit Card Declined'))) < (length('Credit Card Declined') * 4)

关于一个字段内的 MySQL 查询出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6130491/

相关文章:

mysql - mysql中多个值的交集

php - MySQL:使用跨表 SELECT 的准备语句

java - Hibernate 仅从数据库中检索第一条记录,其余为空

mysql - 如何根据可用性从两个表之一查询数据

mysql - Onsen UI 使用 Angular 从 mysql 登录验证后,如何显示我的主屏幕?

java - 在 MySql 工作台上创建新连接指定什么值

mysql - 如何在MySQL中逐行显示记录字符串

mysql - mysql 中 SET autocommit=1 和 START TRANSACTION 之间的区别(我错过了什么吗?)

sql - SQL Server 中几何点的中心或质心

java - MySQL limit子句是如何实现的