从第二个表中提取某些信息但没有“Where”约束

时间:2011-02-25 23:25:17

标签: php mysql database join

我已经能够学习并弄清楚如何在LEFT JOIN中提取信息。现在,我需要从表中提取所有信息,然后使用该表中的user_id从accounts表中提取用户full_name。以下是我正在使用的代码:

代码:

  $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++;
     }

我如何调整它来做我想做的事?

2 个答案:

答案 0 :(得分:1)

有关您的表格的更多信息会很有用,但您需要这样的内容:

SELECT whatever
FROM messages_questions AS mq
JOIN accounts AS a
ON mq.user_id = a.user_id

答案 1 :(得分:1)

可能类似

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