dongliuzhuan1219 2016-07-21 20:34
浏览 26
已采纳

我如何在一个或两个查询中比较结果?

I am trying to fetch last 10 topics from smf database but topics are in two separate tables.

Subject, Post Time and Message ID in smf_messages.
Topic ID and Topic First Message in smf_topics.

I wrote this code to fetch topics but i dont know how to compare these two results.

$result = mysql_query("select subject,id_msg,id_topic from smf_messages");
$result2= mysql_query("select id_first_msg from smf_topics");
if(mysql_num_rows($result)!=0)
{
    while($read = mysql_fetch_object($result))
    {
        if ($read->id_msg==$read->id_topic) {
            echo "<a href='../index.php?topic=" . $read->id_topic . "'>".$read->subject."</a><br>";
        }
    }
  • 写回答

4条回答 默认 最新

  • dousong1992 2016-07-21 20:56
    关注

    You probably want the following query

    select subject, id_msg, id_topic, id_first_msg
    from smf_messages
    left join smf_topics on smf_message.id_msg = smf_topics.id_first_msg
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?