简体   繁体   English

如何在数据库中搜索字符串X次? (MYSQL + PHP)

[英]How can I search the string X times in the DB? (MYSQL + PHP)

Hey, first I use SQL + PHP the type of DB is MYSQL. 嘿,首先我使用SQL + PHP,DB的类型是MYSQL。

I have a column with the many strings, but I want to search the string 08/08/10 if it exists 5 times for example in the column, how do I do it? 我有一列包含很多字符串,但是我想搜索字符串08/08/10(例如,如果该列中存在5次),该怎么办?

** If I will do: **如果我愿意:

SELECT * FROM x WHERE y LIKE '%08/08/10%'

Maybe it exists, but I don't know if 5 times.. 也许它存在,但是我不知道是否有5次。

Thank you very much! 非常感谢你!

select count(1) from x where y like '%08/08/10%' -outputs exact number of rows that have y like '%08/08/10%' select count(1) from x where y like '%08/08/10%'输出具有y的确切行数,例如'%08/08/10%'

no group by needed in this particular case. 在这种特殊情况下,无需分组。

fetch rows where the string exists at least once and use php (eg substr_count) to count occurrences. 提取至少存在一次字符串的行,并使用php(例如substr_count)对事件进行计数。

foreach($db->fetchAll(" where y like '%blah%' ") as $rec)
   if(substr_count($rec->y, "blah") == 5)
       bingo...

it also may help to tell us more about your problem - maybe there are better ways to structure the database 它也可能有助于告诉我们有关您的问题的更多信息-也许有更好的方法来构建数据库

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

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