MySQL Query在一个字段中出现

时间:2011-05-25 20:29:03

标签: mysql sql

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

这样的事情: SELECT * FROM b_orders其中invoice_notes具有“信用卡被拒绝”< 4“

感谢任何帮助。 感谢

3 个答案:

答案 0 :(得分:1)

这听起来像答案: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)

答案 1 :(得分:0)

如下:

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%"

答案 2 :(得分:0)

SELECT * FROM b_orders WHERE invoice_notes LIKE '%Credit Card Declined%' 
AND invoice_notes NOT LIKE REPEAT('%Credit Card Declined%',4);