Solved

MySQL ORDER BY messing up sort

Posted on 2009-07-01
2
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
kevin_u earned 500 total points
ID: 24758926
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
ID: 31598998
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 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