简体   繁体   中英

php search a mysql string results

guys just a quick question. I have a string in a mysql row resembling this

'google.co.nz, stackoverflow.com, facebook.com, grubber.co.nz'


and so on... I would like to search all the rows in the table and check how many times 'facebook.com' shows up in all the rows that are like the ones above so to clarify

my rows look like this

-- id -- user -- likes 
   1      bob     facebook.com, google.co.nz, grubber.co.nz, stackoverflow.com

and i would like to check how many times facebook.com shows up in the whole table (in every row)

Assuming every user can only like the same page once, this should work:

SELECT COUNT(*)
FROM table
WHERE likes REGEXP '[[:<:]]facebook.com[[:>:]]'

PS: Normalize your table, you will run into serious trouble with a layout like this in the very near future!

You could do it in a lazy fashion, issuing a LIKE SQL query :

SELECT count(*) FROM my_table 
WHERE my_table.likes LIKE '%facebook.com%'

This is really (REALLY) not cpu friendly. Especially with large tables

Otherwise, you could use MySQL fulltext indexes feature.

You may find more details in this article

您可以使用like运算符来匹配模式:

SELECT COUNT(id) AS occurences  WHERE likes LIKE "%facebook.com%"

you could use the "LIKE" statement. It goes like this (if you want to know which user likes facebook.com)

SELECT * FROM yourtable
WHERE likes LIKE '%facebook.com%'

Anyway, please consider storing the likes in a 1:n table construct and not as a string

By that you have aa table storing all like-able sites, a user table and a user-likes table storing the assignment.

尝试这个

select count(likes) from yourTable where likes like '%facebook.com%'

Other option. Get the row data in a variable and then use the function preg_match_all ( http://www.php.net/manual/es/function.preg-match-all.php ).

This function returns the number of occurrences of the regular expression passed in the first parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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