Jump to content


Getting an order from query

  • Please log in to reply
2 replies to this topic

#1 SneakyMax

  • Members
  • PipPip
  • Member
  • 11 posts

Posted 27 April 2006 - 11:17 PM

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

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.

#2 KrisNz

  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 28 April 2006 - 12:48 AM

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

"select * from table1 t1, table2 t2 order by t2.population asc"

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

"select * from table1 t1 INNER JOIN table2 t2 on t1.nationid = t2.nationid order by t2.population asc"

#3 SneakyMax

  • Members
  • PipPip
  • Member
  • 11 posts

Posted 28 April 2006 - 02:00 AM

No idea there was an INNER JOIN, looks like what I'm looking for thanks a ton.

Edit: Works perfectly thanks a ton!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users