Need Help using "order by"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webdzine
Forum Newbie
Posts: 20
Joined: Wed Dec 08, 2010 12:40 am

Need Help using "order by"

Post by webdzine »

i have two tables
Table 1: players
Fields: id, teamid, firstname, lastname, position, height, weight, picture
Table 2: playerpositions
Fields: positionid, position

table 1 consists of many players where some players have the same "position"
table 2 consists of 4 unique positons
the field "position" in table 1 uses the values from field "positions" in table 2

my code looks like

Code: Select all

<table cellspacing="3" cellpadding="0" width="300">
     <?php      
$queryclient = "SELECT * FROM players where teamid=$clubid order by position";
$rscat = mysql_query($queryclient) or die('Error, query failed');
while($rowcat=mysql_fetch_object($rscat)){?>	
	<tr>
		<td width="45"><a href="playerprofile.php?pid=<? echo $row->id?>"><img src="<? echo $player_image_path.'/'.$rowcat->picture?>" height="48" width="44"></a></td>
		<td width="200" dir="ltr"><a href="playerprofile.php?pid=<? echo $rowcat->id?>"><? echo $rowcat->firstname.' '.$rowcat->lastname?></a><br><? echo $rowcat->position?></td>	
	</tr>
      <? } ?>  
</table>

but i want to order it by positionid instead of position
how do i create a query that will be able to order by a field in a different table
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Need Help using "order by"

Post by Darhazer »

Use a join

Code: Select all

$queryclient = "SELECT * FROM players INNER JOIN playerpositions USING(position) where teamid=$clubid order by positionid";
webdzine
Forum Newbie
Posts: 20
Joined: Wed Dec 08, 2010 12:40 am

Re: Need Help using "order by"

Post by webdzine »

thank you very much.
works perfectly
Post Reply