Solved

MySQL ORDER BY messing up sort

Posted on 2009-07-01
2
242 Views
Last Modified: 2013-12-13
I got help with a query yesterday that orders by a set of random results. The code is below. The problem I'm having now is a weird one and I'm not entirely sure how to fix it.

I'm pulling records: 5,8,3,6,10,7,11,2,4,9,1
When I loop through the results however I'm getting: 5,8,3,6,1,10,7,11,2,4,9

Notice the `1` was pushed right before the 10?

I cut out a few of the unnecessary pieces of code and threw the ID's I was trying to pull in the query for testing purposes.
mysql: SELECT question_id, question FROM questions WHERE question_id IN (5,8,3,6,10,7,11,2,4,9,1) ORDER BY INSTR('5,8,3,6,10,7,11,2,4,9,1', question_id)

 

<?php

$sql = $this->db->GetAll("

  SELECT

    question_id,

    question

  FROM " . $this->db_prepend . "questions

  WHERE question_id IN (5,8,3,6,10,7,11,2,4,9,1)

  ORDER BY INSTR('5,8,3,6,10,7,11,2,4,9,1', question_id)

");

 

foreach ($sql as $r) {

	echo $r['question_id'] . ' - ';

}

?>

Open in new window

0
Comment
Question by:SOakley54
2 Comments
 
LVL 12

Accepted Solution

by:
kevin_u earned 500 total points
Comment Utility
INSTR does a string match without regard to a delimter.
So 1 matches the 1 in 10.

try this:
ORDER BY INSTR('05,08,03,06,10,07,11,02,04,09,01', lpad(question_id,2,'0')

this makes the leading zeros part of the string and the search string.
0
 

Author Closing Comment

by:SOakley54
Comment Utility
Nice observation. It worked with a small tweak. Thank you!

ORDER BY INSTR('05,08,03,06,10,07,11,02,04,09,01', LPAD(question_id, 2, 0))
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now