![](/img/trans.png)
[英]How do I select one exact substring but not another from a string in mysql?
[英]mysql select count of records from a string which have exact substring
我有以下五個記錄。 我需要獲取“搖滾”的數據。 因此,我需要這樣的輸出,輸出應顯示“ rock”的計數,因此根據上述數據庫,輸出計數應為“ 3”,即Rec 1,Rec 3和Rec 5”。因此該計數不應取“記錄2和記錄4”。
rec1: [u'armenian rock', u' armenian', u' singer-songwriter', u' rock']
rec2: [u'punk', u' armenian rock', u' new wave', u' italian']
rec3: [u'rock']
rec4: [u'japanese', u' okuda tamio related', u" rock'n'roll", u' roots rock']
rec5: [u'test rock', u' armenian', u' singer-songwriter', u" rock"]
嘗試字符串功能
INSTR(str,substr)
在您的查詢中。
像這樣:
SELECT COUNT(*) FROM yourTable WHERE INSTR(yourTable.tags, 'rock') > 0;
另一種選擇是使用
LOCATE(substr,str), LOCATE(substr,str,pos)
請查看mysql文檔:
http://dev.mysql.com/doc/refman/5.1/en/string-functions.html
SELECT (LENGTH(tags) - LENGTH(REPLACE(tags, 'rock', ''))) / LENGTH('rock') AS count FROM your_table
嘗試這個。 是的,看起來腫,但據我所知,沒有其他可能的解決方案。
SELECT SUM((LENGTH(tags) - LENGTH(REPLACE(tags, 'u\' rock', ''))) / LENGTH('u\' rock'))as counting
FROM tableName
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.