Reputation: 337
I have two mySQL tables:
1: id, agent, promoter, promoterrank, prevrank, newrank, promotiontime
2: id, rank
newrank and promoter ranks are INTS that correspond to the ID in table 2 (ex: Trainee = 2, Supervisor = 4, etc...)
I'm trying to INNER JOIN A to B in a way that I can show the actual name of the rank as opposed to the ID related to the rank. I've successfully done this for newrank, but no matter what I try to do for promoterrank it just does not work. I appreciate all the help.
This is the SQL query im using:
SELECT A.*,B.rank as brank,DATE_FORMAT(promotiontime, '%Y-%m-%dT%H:%i:%s0Z') FROM promotions A
INNER JOIN ranks B ON A.newrank = B.id
WHERE agent='".$_POST['agent']."' ORDER BY id DESC
And my PHP:
<td><?php echo $row['brank']; ?></td>
<td><a href="profile.php?user=<?php echo $row['promoter']; ?>"><?php echo $row['promoter']; ?></a></td>
<td><?php echo $row['promoterrank']; ?></td>
Upvotes: 0
Views: 138
Reputation: 133360
You want decode two value for rank (one for newrank and one for promoterrank ) then you should join tha table rank two time eg:
SELECT A.*
, B.rank as newrank
, C.rank as promoterrank
DATE_FORMAT(promotiontime, '%Y-%m-%dT%H:%i:%s0Z')
FROM promotions A
INNER JOIN ranks B ON A.newrank = B.id
INNER JOIN ranks C ON A.promoterrank = C.id
WHERE agent='".$_POST['agent']."' ORDER BY id DESC
anyway you should avoid the use of php var in sql code .. (you are at risk for sqlinjection) so take a look at prepared statement and binding param
Upvotes: 1