2011-05-25 50 views
2

我在名爲b_orders的表中有一個名爲invoice_notes的字段。在此字段中,根據卡被拒絕的次數,「信用卡已被拒收」字樣會顯示多次。我試圖編寫一個查詢來選擇只有短語「Credit Card Declined」出現少於4次的記錄?MySQL查詢字段內出現

事情是這樣的: SELECT * FROM b_orders其中invoice_notes有 「信用卡被拒」 < 4「

任何幫助表示讚賞 感謝

回答

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

這聽起來像是答案:http://pisceansheart.wordpress.com/2008/04/15/count-occurrence-of-character-in-a-string-using-mysql/
Quote:

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) 
0
SELECT * FROM b_orders WHERE invoice_notes LIKE '%Credit Card Declined%' 
AND invoice_notes NOT LIKE REPEAT('%Credit Card Declined%',4); 
+0

這完美地工作!非常感謝你的幫助。 – Keith 2011-05-25 20:42:43

+0

不客氣。 – Johan 2011-05-25 20:56:05