Jump to content

Getting an order from query


SneakyMax

Recommended Posts

Okay, this is my first post so don't go too hard on me...

I've been using php for about 2 months now, got the basics. My problem is that for the thing that I am making online, it uses two different mysql tables to 'simplify' things, so I don't have too many columns.

The first table, for my nation simulation, has these things:
nation id, first name, last name
And the second table has these:
nation id
population

I have a script that lists all of the nations, and in order of different things. Such as nation id and nation name, using "SELECT * from [table1] ORDER BY 'nationid' ASC
To order by nation id

The problem is I don't know how to order it by population, since it's in a different table. The nation ids in table 2 go with the nation ids from table 1, but I don't know how to get the order of population from table 2, then use that order in table 1 to get the name.

Hope it's not too confusing...it might have to do with sql, or mysql_fetch_array probably.
Link to comment
Share on other sites

you can either pull all the information from the second table out along with the first one.

[code]"select * from table1 t1, table2 t2 order by t2.population asc"[/code]

or its more likely you'll want to join the two tables on their common key

[code]"select * from table1 t1 INNER JOIN table2 t2 on t1.nationid = t2.nationid order by t2.population asc"[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.