1
$search_exploded = explode (" ", $search);
foreach($search_exploded as $search_each){
    $x++;
    if($x==1) 
        $construct .="concat_ws(Name, Summary, Description, OpeningHours, Address, Postcode, Keywords) LIKE '%$search_each%' ";
    else
        $construct .="AND concat_ws(Name, Summary, Description, OpeningHours, Address, Postcode, Keywords) LIKE '%$search_each%' ";
}

$construct ="SELECT * FROM Rating, Attraction WHERE $construct" ;
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);

if ($foundnum==0)
    echo "Sorry, there are no matching result for '$search'" ;
else
{
    echo "";
    while($row = mysql_fetch_assoc($run)){
        echo '<a href="details.php?ID=' . $row['AttractionID'] . '"><img class="attractionimg" src="'. $row['ImageUrl'] . '" height="160" width="210" /></a>';
        echo '<div class="a-textcontain">';
        echo '<h2><a href="details.php?ID=' . $row['AttractionID'] . '">' . $row['Name'] . '</a></h2>'; 
        echo '<p class="a-text">' . $row['Summary'] . '</p>';
        echo '</div>';
        echo '<img class="rating" src="'. $row['RatingUrl'] . '" height="25" width="140" alt="Rating"/>';
        echo '<div class="aborderline"></div>';
        break;
    }
}  

我的数据库中有 12 行数据。当我搜索时,它工作正常,但每个搜索结果重复 12 次。我试过放一个“休息”;就像您在上面看到的那样,但这会将搜索限制为只有一个结果...

有人有想法么?


$construct = "SELECT Attraction.*, Type.TypeName, Rating.RatingUrl";

$construct .= "来自景点";

$construct .= "Inner JOIN Type ON Attraction.Type = Type.TypeID";

$construct .= "Inner JOIN Rating ON Attraction.AttractionID = Rating.AttractionID";

$construct .= "WHERE Attraction = $construct" ;

4

1 回答 1

1
SELECT * FROM Rating, Attraction

您正在执行一个多表查询,其中 的每一行都Rating连接到 的每一行Attraction,从而将结果相乘。您将需要LEFT JOIN具有特定连接条件或可能其他内容的查询,具体取决于意图是什么。

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html,http://dev.mysql.com/doc/refman/5.1/en/join _ _ .html

于 2012-12-07T12:35:39.560 回答