PHP在多个表组中搜索MySQL

时间:2014-04-27 04:15:43

标签: php mysql search-engine

我正在构建一个搜索引擎,我想在其中查找两个表。它们根本不相关..我想要的是在这两个表中搜索并导出结果。 这是我得到结果的查询,但如果在'Books'表中找到结果,则输出返回表中所有记录的总和

// explode search words into an array
 $q = $_POST['q'];
 $arraySearch = explode(" ", $q);
 $countSearch = count($arraySearch);
 $b = 0;`

 $query = "SELECT book.title, book.author, store.title, store.address, 
           CONCAT_WS(' ',book.title, book.author, store.title, store.address)
           FROM nt_books book, nt_stores store WHERE (";
  $countFields = count($arrayFields);`

while ($b < $countSearch)
{
  $query = $query."CONCAT_WS(' ',book.title, book.author, store.title, store.address) LIKE '%$arraySearch[$b]%'";
  $b++;
  if ($b < $countSearch)
  {
    $query = $query." AND ";
  }
}
$b = 0;

$query = $query.") AND sem.visible=1
 ORDER BY (
 (
CASE 
    WHEN book.title like '$q%' OR book.author LIKE '$q%' THEN 0
WHEN book.title like '% %$q% %' OR book.title LIKE '% %$q% %' THEN 1
WHEN book.title like '%$q' OR book.title LIKE '%$q' THEN 2
    ELSE 3
    END
)
+(
    CASE WHEN store.title like '$q%' OR store.title LIKE '$q%' THEN 0
    WHEN store.title like '% %$q% %' OR store.title LIKE '% %$q% %' THEN 1
    WHEN store.title like '%$q' OR store.title LIKE '%$q' THEN 1
    ELSE 2
    END
)
 ) DESC";

如何从单独的EACH表中检索结果?到目前为止,这会混合所有记录混乱。

0 个答案:

没有答案