PHP和MySQL:如何从两个表中获取所有数据库条目?

I have the following code which gets data from a table entry and displays some html for each entry. It works fine but i want it to display html for the entries in that table AND another table. I could just duplicate this code and change it a bit to get the entries in a different table but that creates another problem, all the entries from the second table would start at the end of the entree list from the first table. How can I display the entries from both table in the order of most relevant to what the user types in? Here is my code that just displays entries from the first table in order of when they were inserted into the table. Can someone tell me how to display all the entries from two different tables in order of relavace to a user search query? My code so far, I have not been able to get much further than this.

$pagesQuery  = mysql_query("SELECT count(id) FROM(`posts`) WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%'");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);

$currentname = mysql_query("SELECT * FROM `posts` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%' LIMIT $start, 5"); 


while ($row = mysql_fetch_array($currentname)) {
    //recieve relevant data.
    $title = $row[0];
    $desc = $row[13];
    $ID = $row[6];
    $views = $row[3];
    $user = $row[7];
    $type = $row[15];
    //fetch the last id from accounts table.
    $fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
    $lastrow1 = mysql_fetch_row($fetchlast1);
    $lastid1 = $lastrow1[6];
    //acquire the username of postee.
    for ($i1=1; $i1 <= $lastid1; $i1++) { 
        $currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
        while ($row1 = mysql_fetch_array($currentname1)) {
            $username1 = $row1[0];
        }
    }

    //Format Title, description and view count.
    $title2 = rtrim($title);
    $donetitle = str_replace(" ", "-", $title2);
    $donetitle1 = str_replace(".", "", $donetitle);
    $donetitle2 = str_replace("-", "-", $donetitle1);
    $donetitle3 = str_replace(":", "-", $donetitle2);
    $url = "articles/".$ID."/".$donetitle3."";

    $donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
    $donedesc = '';

    if(strlen($desc) > 150) {
        $donedesc = explode( "
", wordwrap( $desc, 150));
        $donedesc1 = $donedesc[0] . '...';                          
    } else {
        $donedesc1 = $desc;                         
    }
    $SRCIMG = '';
    $finviews = number_format($views, 0, '.', ',');
    if($type == '1'){
        $SRCIMG = "img/icons/video.png";
    } else {
        $SRCIMG = "img/icons/article.png";
    }
    //Give  results

    if($row[10] == null){
        $SRC = "img/tempsmall.jpg";
    }else{
        $SRC ="generateThumbnailSmall.php?id=$ID"; 
    }
    echo "<div id = \"feature\">
            <img src=\"$SRC\" alt = \"article thumbnail\" />
            <img src=\"$SRCIMG\" alt = \"icon\" id=\"icondisp\"/>
        </div>
        <div id = \"feature2\">
             <a href= \"$url\" id = \"titletext\" alt = \"article title\">$donetitle</a>
             <p id=\"resultuser\" >$username1</p>
             <p id=\"resultp\">$donedesc1</p>
             <a href = \"sendflag.php?title=$title&url=$url&id=$ID&userid=$user\" id = \"flag\" alt = \"flag\"><img src=\"img/icons/flag.png\"/></a><b id=\"resultview\">$finviews views</b> 
        </div>
        <div id = \"border\"></div>";        
}

Why not try union ?

SELECT * FROM `posts` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%  {$terms}%' 
union
SELECT * FROM `otherposts table` WHERE `title` LIKE '%{$terms}%' OR `descrip` LIKE '%{$terms}%' 
order by yourRelevanceField
LIMIT $start, 5"

Actually it just hit me, I should probably create a new table called SearchElements where I store data needed for a search result. I would create a SearchElement each time an entree is added to either table. still dont understand relevance yet though.

Well you can go for UNION of two tables and try something like this

INSERT INTO table3 SELECT * FROM tabel1 UNION SELECT * FROM tabel2

In this case you ought to use UNION to merge SELECT results from two or more tables.

So you query should look like this:

(SELECT *
FROM `posts`
WHERE `title` LIKE '%{$terms}%'
OR `descrip`  LIKE '%{$terms}%')
UNION
(SELECT *
FROM `posts2`
WHERE `title` LIKE '%{$terms}%'
OR `descrip` LIKE '%{$terms}%')
LIMIT $start, 5

Notice that number and names of columns in both SELECTS should be the same.