home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Ask a Tech Support Question (free)!

Recordset Order

Reply
Get bargains at  »  Dealighted.com
 
Thread Tools Search this Thread
Currently Active Users: 1859
Discussions: 200,923, Posts: 2,379,092, Members: 246,290
Old January 17th, 2003, 12:00 PM   Digg it!   #1 (permalink)
div
Junior Member
 
Join Date: Jan 2003
Posts: 2
Recordset Order

Hi

I wish to pull back a number of records in the form of a recordset.

i.e.
SELECT * FROM directory WHERE id=14723 or id=12212 or id=13221

The problem is the recordset is pulled back in id order
i.e.
12212
13221
14723

rather than the order I need

i.e.
14723
12212
13321

How do I keep control over the order of recordset?

Thanks

Div
div is offline   Reply With Quote
Old January 17th, 2003, 02:14 PM     #2 (permalink)
Member
 
Join Date: Oct 2001
Posts: 196
You can use an ORDER BY clause at the end of the statement to order by whatever criteria you need to use.

Ex-

Select * from person where id = 1300 or id = 1350
order by last_name, first_name


Otherwise, you will have to put the data into a structure on the code side and write a function to order it.
gwinters is offline   Reply With Quote
Old January 17th, 2003, 02:19 PM     #3 (permalink)
Not Really a Member
 
Join Date: Oct 2001
Posts: 25,382
to follow on gwinters post, if you want the last/first names in a particular order you can say

ORDER BY last_name ASC, firstname DESC .. for ascending/descending order

(hope I didn't bork up that sql, I haven't used asc and desc for awhile lol)
__________________
Helicopters don't fly; they vibrate so much and make so much noise that the earth rejects them.
vass0922 is online now   Reply With Quote
Old January 17th, 2003, 02:24 PM     #4 (permalink)
Member
 
Join Date: Oct 2001
Posts: 196
Looks perfect to me and I just used it five minutes ago. I'm at work right now, of course.
gwinters is offline   Reply With Quote
Old January 17th, 2003, 03:18 PM     #5 (permalink)
div
Junior Member
 
Join Date: Jan 2003
Posts: 2
Sorry I didn't make my self clear.

The list isn't in any numerical order - so the order by clause won't work ( i assume).

I need to pull the records by id -

the ids in Select clause can be random (although valid) and must be kept in the order they are generated.

Thanks for the replies so far.
div is offline   Reply With Quote
Old January 17th, 2003, 03:25 PM     #6 (permalink)
Thaumaturge Member
 
howste's Avatar
 
Join Date: Oct 2001
Location: West Haven, Utah
Posts: 15,310
I don't think you can use ORDER BY unless you have something to put in order. Do you have some kind of generation date or time field you can sort by? If not, can you create another field in the table that you can use to sort the data? You don't have to display the field, just use it to sort.
howste is offline   Reply With Quote
Old January 17th, 2003, 03:52 PM     #7 (permalink)
ph34r t3h g04t
 
Whir's Avatar
 
Join Date: Oct 2001
Location: Kingsford, MI
Posts: 19,531
Blog Entries: 7
Is this some kind of form interaction? Could you parse out the query data (selected numbers or text input for the ID) and run a new query for each? I realize that's more overhead, but if you haven't got a field to sort by, and you need them in the order selected...
Whir is offline   Reply With Quote
Old January 17th, 2003, 05:55 PM     #8 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: New Hampshire, USA
Posts: 641
My experience is that queries are automatically ordered in ascending order according to the first field retrieved

is there a primary key for the database? any autonumber fields? how about a now() field in each record in order to capture the time and date the record was entered into the database?

try selecting one of those as the first field of your query if possible

If you cannot sort by any of these fields, you might want to consider adding a field to the underlying table to record now() whenever a record is entered.

this will allow you to sort by the now() field to preserve the data entry order for your needs.
ctaylor is offline   Reply With Quote
Old January 17th, 2003, 06:05 PM     #9 (permalink)
Member
 
Join Date: Oct 2001
Posts: 196
Try this:

select * from table1 where id = 1300
union all
select * from table1 where id = 1250
union all
select * from table1 where id = 1400



That would be the only way that I can think of. If you had a list of dynamic length, then you would have to use sql strings and make your query as you go along. Or you could make a temp table and insert your results into it in the order that you wanted them. That may not work if it wants to order them by ID (you'd end up right where you started), but then you could create a column that uses a timestamp and order by it as suggested above.
gwinters is offline   Reply With Quote
Old January 17th, 2003, 09:48 PM     #10 (permalink)
Senior Member
 
Join Date: Oct 2001
Location: Alberta, Canada
Posts: 563
Quote:
I need to pull the records by id

...you can't use ...ORDER BY id ASC/DESC ???
^hyd^ is offline   Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Most Active Discussions
Is It Just Me? (2854)
Obama the Muslim (13)
Why is Khalid Sheikh Mohammed even .. (9)
Is the PSU I received dead? (10)
windows vista security holes (8)
Foreign voltage (10)
Print spooler problem (13)
HIS HD5770 graphic card question (15)
Install XP pro and a Vista laptop ?.. (9)
Dept. of HS: NSA 'Helped' Develop V.. (15)
A good PSU? (10)
New Computer wont recognize XP disc (7)
Ideal cheap graph card for PC-Gamin.. (15)
EVGA 9800 gtx help with finding a g.. (8)
Recent Discussions
Fire in DVD (0)
radeon x850xt platinum & shader 3 (2)
The NTDVM CPU has encountered an ille.. (24)
[F@H SPAM 11/16/09] ! 1/2 months to r.. (34)
Wireless speakers for PC? (11)
Print spooler problem (13)
Help getting around port 80 for camer.. (2)
Display shows 3x5 inch in middle of s.. (3)
windows vista security holes (8)
monitor will not turn on at all, (1)
World's largest Monopoly Game using G.. (331)
Foreign voltage (10)
FiOS modem/router interfering with ne.. (7)
Browsers wont load websites (2)
Virus Doctor Popup? (1)
Dept. of HS: NSA 'Helped' Develop Vis.. (15)
Install XP pro and a Vista laptop ?? (9)
EVGA 9800 gtx help with finding a goo.. (8)
Modern Warfare For the PC (32)
Problem with speed step/turbo boost? (1)
Modern Warfare 2: Who Bought It? (61)
SIS 740 and Widescreen (8)
Baffling Problem with my CPU/MoBo's. .. (0)
HIS HD5770 graphic card question (15)
Best file format to play on Windows H.. (0)


All times are GMT -4. The time now is 09:52 PM.
TechIMO Copyright 2009 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28