e1seix Posted January 8, 2010 Share Posted January 8, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/ Share on other sites More sharing options...
salathe Posted January 9, 2010 Share Posted January 9, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-991417 Share on other sites More sharing options...
e1seix Posted January 9, 2010 Author Share Posted January 9, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-991433 Share on other sites More sharing options...
fenway Posted January 13, 2010 Share Posted January 13, 2010 You can use SUBSTRING_INDEX() or LOCATE(), but only if you know *exactly* how all of your data is formatted. Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-994369 Share on other sites More sharing options...
e1seix Posted February 20, 2010 Author Share Posted February 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-1015091 Share on other sites More sharing options...
fenway Posted February 23, 2010 Share Posted February 23, 2010 Like so: select substring_index( trim( "www.mysite.com?id=" from "www.mysite.com?id=33&name=Brenda" ), '&', 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-1016919 Share on other sites More sharing options...
zeodragonzord Posted March 1, 2010 Share Posted March 1, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187776-group-by-regex/#findComment-1020102 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.