steven
steven

Reputation: 13537

SQL Three Table Join and sort

So, I have countries, regions and groups.

countries
|cid|name|
regions
|rid|name|cid|
groups
|gid|name|phone|time|rid|

So how can I select each group with its country and region in order:

eg.

|cname|rname|gname|phone|time|
|Australia|nsw|test|1111|whatever|
|Australia|nsw|test2|110|whatever|
|Australia|vic|test3|100|whatever|
|England|London|tes4|010|whatever|

Upvotes: 0

Views: 1897

Answers (1)

James Black
James Black

Reputation: 41858

SELECT c.name, r.name, g.name 
FROM groups g INNER JOIN regions r ON(r.rid=g.rid) 
    INNER JOIN countries c ON(c.cid=r.cid) 
ORDER BY c.name, r.name, g.name;

This should get you most of the way that you want to go.

I didn't know what you wanted to order by.

Upvotes: 4

Related Questions