PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 3, 2022

[FIXED] How to use SQL code to select the latest ID that is not in order (Even though I have sort it)

 July 03, 2022     dom, mysql, select, sql, xampp     No comments   

Issue

So it is a picture that show my ID that is in order if we assume S2 is bigger S10. The order of my ID is S1, S10, S11, S12, S2, S3, S4, S5, S6, S7, S8, S9

I wrote my SQL code like this and the latest ID it return is S9 Just keep in mind that I want the alphabet beside the number. So which SQL code allow me to select the latest ID that is S12 ? PLS help me.

SELECT IdSoalan FROM SOALAN ORDER BY IdSoalan DESC LIMIT 1

Solution

Try this:

SELECT IdSoalan FROM SOALAN ORDER BY LENGTH(IdSoalan) DESC, IdSoalan DESC LIMIT 1

It sorts by length of the column in a descending order and then by the value in a descending order and limits the result to 1. It's called natural sorting in MySQL.



Answered By - MFB
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing