PDA

View Full Version : Combining Data From 2 Tables


tomyknoker
04-14-2007, 01:22 AM
I have the following code

$results = mysql_query("SELECT * FROM `tblmembers` WHERE `member` = 'P'");

if (mysql_num_rows($results) < 1) {
die('No members were approved last month');
}

else {

while ($qry = mysql_fetch_array($results)) {
if (strtotime($qry['loginDateTime']) <= (time() + 86400*31)) {
$login .= 'Name: '.$qry["FirstName"].' '.$qry["LastName"].' ('.date("d/m/Y", strtotime($qry["JoinDate"])).') '.$qry["Email"].''.$qry["r_id"].' ('.$qry["State"].')
';
}
}

The 'r_id' is an ID from another table in my databse, so it outputs the ID number, but I want to pull the FirstName & LastName from that table so if the 'r_id' = 5 and 5 = Joe Bloggs I want to output that, just not sure how to do it, as it means another Query... I can explain further if this is a weak explanantion :)

Horus_Kol
04-14-2007, 01:46 AM
sounds like you want to do a join... this is a method which allows you to combine the information in multiple tables based on a common value between them...

so, for example, you have a table of threads and a table of users, and the table of threads has the user id in there:


SELECT thread_title, username FROM user LEFT JOIN threads USING (user_id)



http://dev.mysql.com/doc/refman/5.1/en/join.html

tomyknoker
04-14-2007, 02:01 AM
So it would go under my inital query? And would be like this...

SELECT `tblreps`, `r_id FROM `ID` LEFT JOIN threads USING (`ID`)