Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

SQL/PHP Sorting results alphanumerically

Featured Replies

Hey all,

 

I'm trying to get a php loop to display records descending by reference by using

 

SELECT * FROM coll_grey order by ref desc

 

But some numbers are into their thousands, so sorting by desc doesn't work. The results display something like this

 

R997

R996

R995

R1002

 

Is there a way to get the 1002 to appear before the 997, so they are treated as real numbers? I guess it's only looking at the first few digits and maybe the R is causing a problem?

 

Thanks guys :)

Sorry bud, SQL considers that a string and not a number, R9 comes before R1 in descending order, just the nature of the beast. Depending how many records you have depends what the best thing to do is. Might be worth making a varchar column for the prefix (e.g. R) and in int column for number (e.g. 1002). Then run a script to split the reference and store the prefix and int separately, but this really does depend on how much data there is, a CRON might be the better option for this.

 

Edit: There's one way which is https://www.copterlabs.com/natural-sorting-in-mysql/ however it all depends if the prefix is always the same length.

Edited by BrowserBugs

  • Author

Sorry mate just seen your reply. Thanks for the link, it helped me get things working with...

 

order by length(ref) desc, ref desc

 

Nice one!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.