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
https://forums.phpfreaks.com/topic/157113-mysql-counting-matches/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.