[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

MySQL ORDER BY messing up sort

Posted on 2009-07-01
2
Medium Priority
?
252 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

650 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