0

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)

4
  • 10
    You're better off normalizing your database first. Commented Sep 7, 2012 at 9:45
  • is it comma separated value? i mean you have columns like this, ID, User and Likes (which is csv)? Commented Sep 7, 2012 at 9:46
  • @knittl yer i know, the stucture is crap it's just for a bit of testing Commented Sep 7, 2012 at 9:57
  • 1
    The problem is that many programs written as a simple prototype end up going into production. It's not a good idea to write bad code even when you're "just testing an idea", because before you know it your bad DB design will end up being the master database and you won't be able to fix it. Commented Sep 7, 2012 at 10:00

6 Answers 6

5

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!

Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't that match notfacebook.com too?
Just normalize! csv fields are almost always a bad idea
Sure, but that's not the question.
true... that's why I upvoted. (just emphasis-sing your PS :p)
1

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

Comments

0

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 a a table storing all like-able sites, a user table and a user-likes table storing the assignment.

Comments

0

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.

Comments

0

Try this

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

Comments

0

You can use the like operator to match the pattern:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.