Search code examples
phpmysqlsqlsmf

How i can compare the result in one query or two?


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>";
        }
    }

Solution

  • 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