If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Simple question about ORDER BY

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-18-03, 08:18
Chief Chief is offline
Registered User
 
Join Date: Jul 2002
Location: Denmark
Posts: 5
Question Simple question about ORDER BY

Hi,

Imagine the following case:

We have 2 tables:

cars: Contains a list of cars
car_owners: Contains a list of car owners

Every car owner is capable of owning one car and in the 'car_owners' table we have foreign key (fk_car_id) to the owners car in the 'cars' table.

I hope you're able to keep up so far!

Now, I can simply fetch a list of car owners ordered by their priority with this SQL:

SELECT * FROM car_owners ORDER BY priority

Easy! But what I really, really want, is to fetch a list of cars ordered by the priority of their owners!

The following doesn't work:

SELECT * FROM cars WHERE id IN ( SELECT fk_car_id FROM car_owners ORDER BY priority )

Because the IN-clause doesn't guarantee this! The cars are just returned in any order if their id is part of the subquery!

What am I missing here???

I Hope somebody can help me out of my confusion about this!
Reply With Quote
  #2 (permalink)  
Old 12-18-03, 08:48
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
select cars.*
from cars INNER JOIN car_owners ON cars.car_id = car_owners.car_id
order by car_owners.pr;

Did you try the query using IN, as I don't think ORDER BY is permitted within a subquery of an IN.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 12-18-03, 08:59
edwinjames edwinjames is offline
Registered User
 
Join Date: Dec 2003
Posts: 74
THIS SHOULD WORK?

SELECT CA.CAR_NAME,CA.PRIORTY FROM CARS CA, CAR_OWNERS CO
WHERE
CA.OWNER_ID = CO.ID
ORDER BY
CO.PRIORTY
Reply With Quote
  #4 (permalink)  
Old 12-18-03, 10:11
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Quote:
select cars.*
from cars INNER JOIN car_owners ON cars.car_id = car_owners.car_id
order by car_owners.pr;
That is the best way to do it.

The only problem you may run into here is if you have a car that nobody currently owns. In that case you will want to use an outer join. Something to the effect of:

SELECT cars.*
FROM cars LEFT OUTER JOIN owners ON cars.car_id = car_owners.car_id
ORDER BY car_owners.priority
Reply With Quote
  #5 (permalink)  
Old 12-18-03, 10:18
edwinjames edwinjames is offline
Registered User
 
Join Date: Dec 2003
Posts: 74
WOULD THAT NOT BE THE SAME AS

SELECT CA.CAR_NAME,CA.PRIORTY
FROM CARS CA, CAR_OWNERS CO
WHERE
CA.OWNER_ID(+) = CO.ID
ORDER BYCO.PRIORTY
Reply With Quote
  #6 (permalink)  
Old 12-18-03, 10:30
Teddy Teddy is offline
Purveyor of Discontent
 
Join Date: Mar 2003
Location: The Bottom of The Barrel
Posts: 6,075
Quote:
Originally posted by edwinjames
WOULD THAT NOT BE THE SAME AS

SELECT CA.CAR_NAME,CA.PRIORTY
FROM CARS CA, CAR_OWNERS CO
WHERE
CA.OWNER_ID(+) = CO.ID
ORDER BYCO.PRIORTY
depends on the DB. I believe yours is Oracle specific?
Reply With Quote
  #7 (permalink)  
Old 12-18-03, 10:36
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Quote:
Every car owner is capable of owning one car and in the 'car_owners' table we have foreign key (fk_car_id) to the owners car in the 'cars' table.
Given this statement, I do not believe owner_id would be a foreign key in the car's table, as it would be redundant.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #8 (permalink)  
Old 12-18-03, 11:38
edwinjames edwinjames is offline
Registered User
 
Join Date: Dec 2003
Posts: 74
Quote:
Originally posted by Teddy
depends on the DB. I believe yours is Oracle specific?
yes apologies. mine was oracle specific.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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