On Saturday, April 02, 2005 01:51, ON.KG wrote:

> Hi Everybody
> 
> I need to group records by regular expression
> 
> Example:
> 
> Table has column - names (just example)
> records are:
> 
> id    names         time
> 1     John, Max     12:15
> 2     Max, Jeff     15:55
> 3     Ken           20:45
> 
> I need to get grouped records by name "Max" in column "names"
> 
> SELECT COUNT(*)
> FROM my_table
> GROUP BY <reg_expression>

Are you just looking for a count of names that contain "max"?

SELECT COUNT(*) FROM my_table WHERE names REGEXP 'max';

or

SELECT COUNT(*) FROM my_table WHERE names LIKE '%max%';

If you are really trying to group the names you can do this with 
a regex but this will only give you at most three rows (two if 
the column doesn't allow nulls). This is because the regex can 
only evaluate to true, false, or null. If you want just the ones 
that contain Max grouped together and everything else separate 
you could rig it with an if.

SELECT names, COUNT(*) FROM my_table 
GROUP BY if(names REGEXP 'max', -1, names)

The -1 would need to be replaced by a value that will not exist 
in the table. This is why I said it is rigged.

Maybe if you could explain why you are trying to do this, someone
could help you come up with a better solution.
 
> Could somebody help me?
> 
> Thanx

-- 
Tom Crimmins
Interface Specialist
Pottawattamie County, Iowa

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to