Pulling certain information from second table but without a "Where" constraint
I have been able to learn and figure out how to pull information doing LEFT JOIN. Now, I need to pull 开发者_JAVA技巧all information from a table and then use the user_id from that table to pull the users full_name from the accounts table. Below is the code I am using:
Code:
$query="SELECT * FROM messages_questions ORDER BY id ASC";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
echo "";
$i=0;
while ($i < $num) {
$messages=mysql_result($result,$i,"messages_title");
$asker = mysql_result($result,$i,"user_id");
$comp = mysql_result($result,$i,"comp_id");
echo "<div id=container><br><div id=message>$messages<br>Asked by $asker</div>
</div>";
echo "";
$i++;
}
How can I tweak it to do what I want to do?
More information about your tables would be useful, however you need something like this:
SELECT whatever
FROM messages_questions AS mq
JOIN accounts AS a
ON mq.user_id = a.user_id
Probably something like
SELECT mq.messages_title,mq.user_id,mq.comp_id, a.full_name
FROM messages_questions mq
LEFT JOIN accounts a ON a.user_id = mq.user_id
ORDER BY mq.id ASC
精彩评论