rugzo Posted September 8, 2009 Share Posted September 8, 2009 hi all, how can i sum the second value in a mysql field after the delimiter? assume i have a column--> xxx 1_34 34_3 23_32 ... i want to sum the value after the "_" delimiter... i tried like that but no success --> "select sum(substring_index('score','_',-1)) from udf... Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/ Share on other sites More sharing options...
Maq Posted September 8, 2009 Share Posted September 8, 2009 Considering that you're trying to add varchars together, you may have to CAST the substring before operation. Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-914990 Share on other sites More sharing options...
rugzo Posted September 8, 2009 Author Share Posted September 8, 2009 i just want to sum the second values in the column... 1_3 3_3 3-4 3+3+4=12 Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-915002 Share on other sites More sharing options...
rugzo Posted September 8, 2009 Author Share Posted September 8, 2009 i tried this but no success, it gives me 0 and no errors... "select sum(cast(substring_index('score','_',-1) as binary))... Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-915015 Share on other sites More sharing options...
Maq Posted September 8, 2009 Share Posted September 8, 2009 It would be "as UNSIGNED". Are you receiving errors? Have you echoed your query to see what's actually going on? Echoed mysql_error()? Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-915032 Share on other sites More sharing options...
rugzo Posted September 8, 2009 Author Share Posted September 8, 2009 Please tell me how to explode and sum a second value in a field if you now... i don't get any errors, i just get a sero "0" Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-915043 Share on other sites More sharing options...
fenway Posted September 10, 2009 Share Posted September 10, 2009 Make sure your expression for substring_idnex() actually returns what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/173591-sum-substring_index/#findComment-916294 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.