Jump to content

GROUP BY String


SalientAnimal

Recommended Posts

Hi Guys/Gals,

 

Is it possible to perform a Group By (Part of String)?

 

Basically, I have a query where in my WHERE CLAUSE I have the LIKE statement. I am then grouping by a few fields and would like one of the GROUPs to be parts of the WHERE.

 

EG.

SELECT COUNT(*)
, field1
, field2
FROM tablename
WHERE field1 like '%TEXTSTRING%'
OR field1 like '%ANOTHERTEXTSTRING%' 
GROUP BY 

(And this is where I don't know how to GROUP BY  '%TEXTSTRING%' and '%ANOTHERTEXTSTRING%'

 

Link to comment
https://forums.phpfreaks.com/topic/268109-group-by-string/
Share on other sites

Ok so I have managed to get a bit of a solution to this, now I am left with another issue though.

 

SELECT COUNT(*)
, field1
, field2
FROM tablename
WHERE field1 like '%TEXTSTRING%'
OR field1 like '%ANOTHERTEXTSTRING%' 
GROUP BY 
CASE
WHEN field1 like [color=red]'%TEXTSTRING%'[/color] THEN [color=red]'GROUPING 1'[/color]
WHEN field1 like [color=red]'%TEXTSTRINGSIMILARTO1%' [/color] THEN [color=red]'GROUPING 1'[/color]
WHEN field1 like [color=red]'%ANOTHERTEXTSTRING%'[/color] THEN[color=red] 'GROUPING 2[/color]ELSE NULL
END

What I need to do now is display the renamed field value instead of the exsting field value.

Link to comment
https://forums.phpfreaks.com/topic/268109-group-by-string/#findComment-1375997
Share on other sites

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.