搜索结果未显示

时间:2014-06-03 14:16:32

标签: php mysql search group-by

我正在尝试更新我们的搜索过程,以便它可以更好地运行,直到我们的网站在接下来的6-7个月内完成全面改造。目前,它已设置为使用项目的名称,编号,颜色和描述查看表格。

无论出于何种原因,它都没有那么好用,并且遗漏了很多应该包含的结果(并且有意义)。因此,为了解决这个问题,我添加了两个新表:pro_item_keyword和pro_item_keyword_look_up。

关键字表格包含关键字和Keyword_ID。查找表具有ID和Keyword_ID。在添加关键字表时,我遵循与其他选择相同的设置并加入文件(一直运行良好,只是没有检索所有结果)。

一旦我添加了关键字表格的行并进行了搜索,它就会告诉我符合我搜索内容的项目数量,但实际上并没有显示任何项目。我基于处理颜色的线基于它,因为颜色实际上没有显示在结果页面上(并且关键字也不是)。我知道MySQL表连接到关键字表因为额外的结果数量,我可以查看pro_search_log表,看看我搜索了什么以及总结果。

我不明白为什么它在我添加其他表格之前工作正常。表格设置应该是我可能错过的具体内容吗?或者我编码了吗?任何帮助或建议都会很棒!先感谢您!

//This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.
$rows_per_page = 25;
$lastpage      = ceil($numrows/$rows_per_page);

//This code checks that the value of $pageno is an integer between 1 and $lastpage.
$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} 

//This code will construct the LIMIT clause for the sql SELECT statement.
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

//Now we can issue the database query and process the result.
if ($vid) {
//This code will count how many rows will satisfy the current query.
$query = "SELECT
    coas50.pro_item.Image,
    coas50.pro_item.Description,
    coas50.pro_long_description.Long_Description,
    coas50.pro_item.Item_Number,
    coas50.pro_color.Swatches,
    coas50.pro_color.Color
    coas50.pro_item_keyword.Keyword
 FROM pro_item
  LEFT JOIN pro_videos
  ON pro_videos.Item_ID = pro_item.ID
  JOIN coas50.pro_long_description_look_up
    ON coas50.pro_long_description_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_long_description
    ON coas50.pro_long_description.ID=coas50.pro_long_description_look_up.Description_ID
  JOIN coas50.pro_color_look_up
    ON coas50.pro_color_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_color
    ON coas50.pro_color.ID=coas50.pro_color_look_up.Color_ID
  JOIN coas50.pro_item_keyword_look_up
    ON coas50.pro_item_keyword_look_up.ID=coas50.pro_item.ID
  JOIN coas50.pro_item_keyword
    ON coas50.pro_item_keyword.Keyword_ID=coas50.pro_item_keyword_look_up.Keyword_ID
 WHERE 
   pro_videos.Video_ID = '$vid' 
  GROUP BY coas50.pro_item.ID DESC $limit";
} else {

$query = "SELECT
    coas50.pro_item.Image,
    coas50.pro_item.Description,
    coas50.pro_long_description.Long_Description,
    coas50.pro_item.Item_Number,
    coas50.pro_color.Swatches,
    coas50.pro_color.Color
    coas50.pro_item_keyword.Keyword
  FROM coas50.pro_item
  JOIN coas50.pro_long_description_look_up
    ON coas50.pro_long_description_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_long_description
    ON coas50.pro_long_description.ID=coas50.pro_long_description_look_up.Description_ID
  JOIN coas50.pro_color_look_up
    ON coas50.pro_color_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_color
    ON coas50.pro_color.ID=coas50.pro_color_look_up.Color_ID
  JOIN coas50.pro_item_keyword_look_up
    ON coas50.pro_item_keyword_look_up.ID=coas50.pro_item.ID
  JOIN coas50.pro_item_keyword
    ON coas50.pro_item_keyword.Keyword_ID=coas50.pro_item_keyword_look_up.Keyword_ID
 WHERE 
    coas50.pro_item.Description LIKE '%$search%'
    OR coas50.pro_long_description.Long_Description LIKE '%$search%'
    OR coas50.pro_item.Item_Number LIKE '%$search%'
    OR coas50.pro_color.Swatches LIKE '%$search%'
    OR coas50.pro_color.Color LIKE '%$search%'
    OR coas50.pro_item_keyword.Keyword LIKE '%$search%'
    GROUP BY coas50.pro_item.ID  DESC $limit";

}

$result = mysql_query($query);
$search_noslash = stripslashes($search);
$total_results = (!$vid) ? "<h4>$numrows product results for '$search_noslash'</h4>" :    "<br />";

$XX = "<div id='products'>
<b class='rtop'>
    <b class='r1'></b> <b class='r2'></b> <b class='r3'></b> <b class='r4'></b>
</b>
<table id='results' width='100%' align='center' cellpadding='0' cellspacing='0'>
    <tr>
    <td id='results_head'><center>$total_results
    <div id='pro_search'>
        <form method='get' action='product_results.php'> 
        <input id='searchBox' title=\"Please enter a keyword, item number, color, or color code\" name=\"search\" type=\"text\" size=\"15\" maxlength=\"40\" value=\"Search Again\" onblur=\"this.value = this.value || this.defaultValue;\" onfocus=\"this.value == this.defaultValue && (this.value = '');\">
        <input type='image' src='../template_imgs/search_bn.gif' width='52' height='26' id='fetch' title='fetch'>
        </form>
    </div>
</table>
<br />
    <b class='rbottom'>
<b class='r4'></b><b class='r3'></b><b class='r2'></b><b class='r1'></b>
</b>
</div>"; 

2 个答案:

答案 0 :(得分:2)

我不相信结果的数量可以表明您的查询是正确的。我会运行大量搜索,我知道答案,以确定它是否正常工作,无论何时让它工作。使用只有一个或两个产品的异常属性,例如,如果您只有一个淡紫色项目。

但回到你的加入。这是您遇到问题的查询,对吧?

$query = "SELECT
coas50.pro_item.Image,
coas50.pro_item.Description,
coas50.pro_long_description.Long_Description,
coas50.pro_item.Item_Number,
coas50.pro_color.Swatches,
coas50.pro_color.Color
coas50.pro_item_keyword.Keyword
FROM coas50.pro_item
JOIN coas50.pro_long_description_look_up
    ON coas50.pro_long_description_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_long_description
    ON coas50.pro_long_description.ID=coas50.pro_long_description_look_up.Description_ID
  JOIN coas50.pro_color_look_up
    ON coas50.pro_color_look_up.Item_ID=coas50.pro_item.ID
  JOIN coas50.pro_color
    ON coas50.pro_color.ID=coas50.pro_color_look_up.Color_ID
  JOIN coas50.pro_item_keyword_look_up
    ON coas50.pro_item_keyword_look_up.ID=coas50.pro_item.ID
  JOIN coas50.pro_item_keyword
    ON coas50.pro_item_keyword.Keyword_ID=coas50.pro_item_keyword_look_up.Keyword_ID
 WHERE 
    coas50.pro_item.Description LIKE '%$search%'
    OR coas50.pro_long_description.Long_Description LIKE '%$search%'
    OR coas50.pro_item.Item_Number LIKE '%$search%'
    OR coas50.pro_color.Swatches LIKE '%$search%'
    OR coas50.pro_color.Color LIKE '%$search%'
    OR coas50.pro_item_keyword.Keyword LIKE '%$search%'
    GROUP BY coas50.pro_item.ID  DESC $limit";

我认为如果关键字表遵循相同的问题,问题可能出在联接的关键字部分 命名约定作为其余表。

JOIN coas50.pro_item_keyword_look_up
ON coas50.pro_item_keyword_look_up.ID=coas50.pro_item.ID

不应该是

JOIN coas50.pro_item_keyword_look_up
ON coas50.pro_item_keyword_look_up.item_ID=coas50.pro_item.ID

答案 1 :(得分:0)

首先,您滥用GROUP BY。它用于控制聚合(摘要)查询。如果您没有COUNT(*)MAX(col)等任何聚合函数,GROUP BY会在MySQL中执行不可预测的操作,并在标准SQL中失败。阅读GROUP BYORDER BY

其次,你不要期望这个很快,对吧?一系列OR - 加入COLUMN LIKE '%match%函数确实非常慢。

第三,请记住JOIN运算符会消除不匹配的行。您可能会或可能不想要LEFT JOIN

如果我是你,我会逐步建立你的查询来解决这个问题。

从根表和关键字stuff开始。

SELECT
    coas50.pro_item.ID,
    coas50.pro_item.Image,
    coas50.pro_item.Description,
    coas50.pro_item_keyword.Keyword
  FROM coas50.pro_item
  JOIN coas50.pro_item_keyword_look_up
    ON coas50.pro_item_keyword_look_up.ID=coas50.pro_item.ID
  JOIN coas50.pro_item_keyword
    ON coas50.pro_item_keyword.Keyword_ID=coas50.pro_item_keyword_look_up.Keyword_ID
 WHERE 
    coas50.pro_item.Description LIKE '%$search%'
    OR coas50.pro_item_keyword.Keyword LIKE '%$search%'

了解你如何做到这一点。确保结果符合您的期望。然后一次添加。