Jump to content

MySQL Counting Matches?


RussellReal

Recommended Posts

lets say I have a table and it has the following results:

 

__________________________________________________

ID | STRING

--------------------------------------------------

1 | Hey, how are you?

2 | Hey, you said 'hey'!

 

and I wanted to use mysql regex to match 'hey' for example:

SELECT * FROM `table` WHERE `STRING` REGEXP 'hey'

 

I know I could do this other ways.. but I want to know how I can order by the number of 'hey's in the string.. because reading 150 something rows into php and then doing the sorting in PHP will be retarded.. it must be possible I just searched mysql.com up and down and havn't found anything of use..

 

Thanks guys, appreciate the time :)

Link to comment
Share on other sites

I don't think you can do that with just SQL.

Well, not exactly, but you can "cheat" by using REPLACE() to replace "hey" with a single character, and then count the difference in lengths, knowing the length of your target string.  It's ugly, but it works like a charm.  I've used this before to count spaces or commas.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.