Jump to content

GROUP BY Regex


e1seix

Recommended Posts

I've got one for you guys.

 

If I have a select statement to search a table in mysql. I want to group by PART of a column data... the data being a URL.

 

Row 1, Column value: "www.mysite.com/index.php?id=1&code=ABC"
Row 2, Column value: "www.mysite.com/index.php?id=1&code=BCD"
Row 3, Column value: "www.mysite.com/index.php?id=2&code=CDE"
Row 4, Column value: "www.mysite.com/index.php?id=3&code=DEF"
Row 5, Column value: "www.mysite.com/index.php?id=5&code=EFG"
Row 6, Column value: "www.mysite.com/index.php?id=5&code=FGH"

 

So I would like to use a select statement to group the "id" value of the Column, whereby it will return the results... 1,2,3,5.

 

Can it be done?

Link to comment
Share on other sites

Can it be done? Sure, not with REGEXP though (you'd need to use string functions).  However, you'd be much better off extracting that ID number and storing it in another column which can be indexed.

 

How would one do this? ie. The extraction part...

Link to comment
Share on other sites

  • 1 month later...

Question:

 

If I'm using one of these...

 

how for example would you code it if you wanted to extract the value of "id" from the following rows that contains a url in the column "URL"

 

row 1, column value: "www.mysite.com?id=33&name=Brenda"

row 2, column value: "www.mysite.com?id=120&name=Bradley"

row 3, column value: "www.mysite.com?id=1023&name=Bobby"

 

What way is the code constructed so that for each loop of the statement, I get the value of 33, 120 & 1023 to define as a variable and implement into a different column of my database separately.

 

All help appreciated.

Link to comment
Share on other sites

You can so something like:

 

SELECT field1, field2 AS myAlias FROM tableName GROUP BY myAlias;

 

You'll need to extract the ID from field2 and create an alias for it to be used in the GROUP BY clause.  Yes, it's better to have that ID in a separate column.  Perhaps you can create a TRIGGER that will insert the ID into an ID column whenever a new row is inserted into this table.  If not, have your PHP code parse it out and insert the ID that way.

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.