EchoFool Posted June 19, 2009 Share Posted June 19, 2009 Hey, I have a field in my database which is a text format and shows a string like this: Field name: ranks 12,34,252,3,52,3,253 Now what i want to do, is order rows in DESC order by the first number only in that sentence... so some how only order by the number before the , symbol. Is that possible in an order by ? Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/ Share on other sites More sharing options...
Ken2k7 Posted June 20, 2009 Share Posted June 20, 2009 I don't think so or it'll be a heck of a messy SQL. Read up on DB normalization. You should never really put CSV data in a single DB field. When you're stuck with a problem like that, you know you need to organize the data better. Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860013 Share on other sites More sharing options...
EchoFool Posted June 20, 2009 Author Share Posted June 20, 2009 Well the alternative is to have unlimited uknown amount of fields which is deffinately not smart. The list: 12,45,636,346,2 Each number represents the score from each week, as you never know how many weeks the person runs for, its inefficient to make a table with: Week1,Week2,Week3,Week4,Week5,Week6 etc The table would have hundreads of columns =/ And as you can guess i need to ORDER BY on the first week which would be the first number only. Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860209 Share on other sites More sharing options...
Ken2k7 Posted June 20, 2009 Share Posted June 20, 2009 The way you're storing data is not smart either. Why not create a new table that contains the field you want. Something like Table id field_name . . . ? Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860233 Share on other sites More sharing options...
EchoFool Posted June 20, 2009 Author Share Posted June 20, 2009 you've lost me now..? how would i know how many fields to make because it could range from a few scores needed to hundreads? Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860235 Share on other sites More sharing options...
Ken2k7 Posted June 20, 2009 Share Posted June 20, 2009 Doesn't matter. You need more fields, INSERT it into that table. Essentially, that table just holds your fields. For example, the table above can hold the following info - id field_name 1 Week 1 2 Week 2 3 Week 3 . . . Those will be the fields. Need more, add more. Then back in your other table, you can use the id as reference to the field_name. Read the sticky topics in this forum. There is one for DB normalization. Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860239 Share on other sites More sharing options...
EchoFool Posted June 20, 2009 Author Share Posted June 20, 2009 Hmm think i just had a brain wave when reading your post.. what baout: ID | WeekNumer | Score 1 | 1 | 23 1 | 2 | 263 Thank you ! For helping me think out of the box I think this will work now ! Cheers Ken ! Quote Link to comment https://forums.phpfreaks.com/topic/162930-solved-order-by-first-number-in-a-string/#findComment-860240 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.