I’m having trouble with this SQL:
$sql = mysql_query("SELECT $menucompare ,
(COUNT($menucompare ) * 100 / (SELECT COUNT( $menucompare )
FROM data WHERE $ww = $button )) AS percentday FROM data WHERE $ww >0 ");
$menucompareis table fields names what ever field is selected and contains data bellow$buttonis the week number selected (lets say week ‘6’)$wwtable field name with row who have the number of week ‘6’
For example, I have data in $menucompare like that:
123456bool
521478bool
122555heel
147788itoo
and I want to select those, who have same word in the last of the data and make percentage.
The output should be like that:
- bool —
50%(2 entries) - heel —
25%(1 entry) - itoo —
25%(1 entry)
Any clearness to my SQL will be very appreciated.
I didn’t find anything like that around.
Well, keeping data in such format probably not the best way, if possible, split the field into 2 separate ones.
First, you need to extract the string part from the end of the field.
I’ll assume, that numeric part is fixed:
If you’ll have
regexp_replacefunction, thensubstr(entry, 7)should be replaced toregexp_replace(entry, '^[0-9]*', '')to achieve the required result.Variant with
substrcan be tested here.