简体   繁体   English

mysql从具有确切子字符串的字符串中选择记录数

[英]mysql select count of records from a string which have exact substring

I have the five records which is like below. 我有以下五个记录。 I need to fetch the data which have 'rock'. 我需要获取“摇滚”的数据。 So I need the output like this, the output should display the count of 'rock', so according to the above database the output count should be "3" ie, rec 1, rec 3 & rec 5". So the count should not take "rec 2 & rec 4". 因此,我需要这样的输出,输出应显示“ 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"]

Try the string function 尝试字符串功能

INSTR(str,substr)

In your query. 在您的查询中。

Something like this: 像这样:

SELECT COUNT(*) FROM yourTable WHERE INSTR(yourTable.tags, 'rock') > 0;

Another option could be the use of 另一种选择是使用

LOCATE(substr,str), LOCATE(substr,str,pos)

Please have a look into the mysql documentation: 请查看mysql文档:

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html 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

Try this. 尝试这个。 Yes, looks bloated but as far as i know there is no any other possible solution. 是的,看起来肿,但据我所知,没有其他可能的解决方案。

SELECT SUM((LENGTH(tags) - LENGTH(REPLACE(tags, 'u\' rock', ''))) / LENGTH('u\' rock'))as counting 
FROM tableName

FIDDLE 小提琴

You can use this: 您可以使用此:

SELECT COUNT(*) FROM Table1 WHERE INSTR(Table1.tags, '\' rock') > 0;

fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM