Current location: Hot Scripts Forums » Programming Languages » PHP » Query results get ordered by MySQL


Query results get ordered by MySQL

Reply
  #1 (permalink)  
Old 08-11-05, 08:24 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Query results get ordered by MySQL

Problem: query results get ordered even though I don't want them to get.

Code:
SELECT * FROM person WHERE persID IN (202,173,437);
The above will get the result rows in ascending persID order - 173,202,437. I would like to get the result rows in the same order I've listed them in IN-part: 202,173,437.

Is it possible to tell MySQL to get the rows in my order by changing that query somehow or do I have to use another approach?

I tried the following with no success:

Code:
SELECT *,'' AS dumb FROM person WHERE persID IN (202,173,437) ORDER BY dumb;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 08-11-05, 11:04 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

Try ORDER BY persID
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 08-11-05, 11:58 AM
TizMe TizMe is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Give this a whirl.

SELECT * FROM person WHERE persID IN (202) UNION SELECT * FROM person WHERE persID IN (173) UNION SELECT * FROM person WHERE persID IN (437);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 08-11-05, 12:01 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
Quote:
Originally Posted by dennispopel
Hello,

Try ORDER BY persID
the results are being sorted as if he used that.. but he does NOT want that!
he wants the order to be as provided in the IN() ..
I've tried similar query here and it's also sorting it in ascending order.I am not sure why it's behaving like this..


edit: UNION is only for MySQL 4.x and above.. and I guess they would be slow..
also, if you are having one value why use IN() ?
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 08-11-05 at 12:05 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 08-12-05, 01:05 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Little more explanation:

I have tables 'person' and 'group'. The 'person' -table contains usual personal data (name, address etc.) and the group should be a linker table with only 3 columns: groupID, groupOrderNum and group_persID.

The idea is to collect groups of persons: in each group there can be a number of different persons (identified by groupID and persID) and the table should "remember" the order in which the user added the persons to the group. This cannot be alphabetical order and that's why I have the groupOrderNum -field which tells the person's position in the group.

The problem appears when I am doing a html UI for this database. I have two forms on the same page: one for person list (where user can pick up any person) and another for the group which shows the persons user has added to the group. Altough PHP maintains the correct order of persIDs in a temporary array, when I update the group list - after the user has added a new person in it - with the above query, the listing gets in different order than in which the user actually added the persons.

Last edited by darkfreak; 08-12-05 at 01:24 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 08-12-05, 01:32 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by TizMe
Give this a whirl.
SELECT * FROM person WHERE persID IN (202) UNION SELECT * FROM person WHERE persID IN (173) UNION SELECT * FROM person WHERE persID IN (437);
Thanks. This does the trick, even though it's not very elegant if I have, say, 50-100 persons in the group

As NeverMind pointed out, this could be simplified a bit by not using the IN:

Code:
SELECT * FROM person WHERE persID=202 UNION SELECT * FROM person WHERE persID=173 UNION SELECT * FROM person WHERE persID=437;
Strangely, even the query below will order the result:

Code:
SELECT * FROM person WHERE persID=202 OR persID=173 OR persID=437;
I guess this is some MySQL query-optimization feature...?

Last edited by darkfreak; 08-12-05 at 02:21 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL Distinct Query problem FunkyM PHP 20 02-28-07 09:33 AM
mysql query problem (very important for me...) pedroso PHP 5 07-26-05 02:09 AM
Declared Functions skipper23 PHP 4 12-17-03 11:06 AM
index page not showing up skipper23 PHP 3 12-15-03 02:10 PM
Show query results by in a different way mdhall PHP 4 11-09-03 12:18 PM


All times are GMT -5. The time now is 04:39 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.