PDA

View Full Version : How exactly do SQL Subqueries work?


Jaxel
04-21-2009, 08:34 AM
I'm trying to figure this one out... I have one query... that returns a set of rows.

$villages = $vbulletin->db->query_read("
SELECT conquest_players.*, conquest_nations.*, user.username
FROM conquest_players AS conquest_players
LEFT JOIN conquest_nations AS conquest_nations ON conquest_players.nationID = conquest_nations.nationID
LEFT JOIN user AS user ON conquest_players.forumID = user.userid
ORDER BY user.username ASC
");

I am pulling each row with a while loop...

while ($village = $vbulletin->db->fetch_array($villages))
{
$intel = $vbulletin->db->query_first("
SELECT * FROM conquest_intel
WHERE playerID = ".$player[playerID]."
AND villageID = ".$village[playerID]."
");

eval('$villagesHTML .= "' . fetch_template('conquest_villages_bit') . '";');
}

This works great, but the problem is that if I am paginating the results by 30 listings, then the $intel query adds an extra 30 queries per page. I'm wondering if there was a way I could combine the two SQL statements together