字段中的MySQL查询发生率

问题描述

| 我在名为b_orders的表中有一个名为invoice_notes的字段。在此字段中,“信用卡被拒绝”字样会显示几次,具体取决于信用卡被拒绝的次数。我正在尝试编写查询以仅选择短语“信用卡被拒绝”出现少于4次的记录? 像这样: SELECT * FROM b_orders,其中invoice_notes的信用卡被拒绝\“ <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)
    ,怎么样:
SELECT * FROM b_orders 
WHERE invoice_notes LIKE \"%Credit Card Declined%\" 
   AND invoice_notes NOT LIKE \"%Credit Card Declined%Credit Card Declined%Credit Card Declined%Credit Card Declined%\"
    ,
SELECT * FROM b_orders WHERE invoice_notes LIKE \'%Credit Card Declined%\' 
AND invoice_notes NOT LIKE REPEAT(\'%Credit Card Declined%\',4);
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...