SQL自定义搜索功能:无法连接两个表

时间:2011-05-30 03:40:04

标签: php jquery search

  // grab the search types.
  $searchSQL = "SELECT  * FROM jsprofile WHERE ";

  $types = array();
  $types[] = isset($_GET['sicno'])?"`name` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['sname'])?"`icno` LIKE '%{$searchTermDB1}%'":'';
  $types[] = isset($_GET['sgender'])?"`gender` LIKE '%{$searchTermDB2}%'":'';

  $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

  if (count($types) < 1)
     $types[] = "`icno` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

      $andOr = isset($_GET['matchall'])?'AND':'AND';
  $searchSQL .= implode(" {$andOr} ", $types) . "  ORDER BY `icno`"; // order by title.

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

//  if (mysql_num_rows($searchResult) < 1) {
  ///   $error[] = "The search term provided {$searchTerms} yielded no results.";

从这一点来看,一切正常。我能够搜索...但是当我试图加入两张桌子时..

  // grab the search types.
  $searchSQL = "SELECT  * FROM jsprofile INNER JOIN medi WHERE ";

  $types = array();
  $types[] = isset($_GET['sicno'])?"`name` LIKE '%{$searchTermDB}%'":'';
  $types[] = isset($_GET['sname'])?"`icno` LIKE '%{$searchTermDB1}%'":'';
  $types[] = isset($_GET['sgender'])?"`gender` LIKE '%{$searchTermDB2}%'":'';
  $types[] = isset($_GET['medical'])?"`medical` LIKE '%{$searchTermDB2}%'":'';

  $types = array_filter($types, "removeEmpty"); // removes any item that was empty (not checked)

  if (count($types) < 1)
     $types[] = "`icno` LIKE '%{$searchTermDB}%'"; // use the body as a default search if none are checked

      $andOr = isset($_GET['matchall'])?'AND':'AND';
  $searchSQL .= implode(" {$andOr} ", $types) . "  ORDER BY `icno`"; // order by title.

  $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");

//  if (mysql_num_rows($searchResult) < 1) {
  ///   $error[] = "The search term provided {$searchTerms} yielded no results.";

当我这样做时......似乎没有出现查询有什么问题吗?

2 个答案:

答案 0 :(得分:1)

你应该检查MySQL JOIN语法(http://dev.mysql.com/doc/refman/5.0/en/join.html

你需要在那里放一个ON子句

这一行:

$searchSQL = "SELECT  * FROM jsprofile INNER JOIN medi WHERE ";

应该是这样的:

$searchSQL = "SELECT  * FROM jsprofile INNER JOIN medi On jsprofile.[key] = medi.[key] WHERE ";
显然,将您的密钥名称替换为该查询

答案 1 :(得分:1)

首先:

$searchSQL = "SELECT  * FROM jsprofile INNER JOIN medi WHERE ";

...仅在MySQL上有效,其中INNER JOIN缺少链接表的标准被解释为CROSS JOIN - 生成笛卡尔积。这意味着它将生成行,但每个medi表都会与每个jsprofile表相关,反之亦然。

只要有基于搜索条件的记录,查询就会返回结果 - 只是不好的结果。所以你必须解释“没有任何结果”才能得到有用的答案......

其次,使用:

$searchSQL = "SELECT  * FROM jsprofile ... WHERE 1 = 1 ";

...将允许您连接其他WHERE子句,只要它们以“AND ...”开头:

$types[] = isset($_GET['sicno'])?" AND `name` LIKE '%{$searchTermDB}%'":'';

...不需要你已经定义的逻辑。

第三,使用Full Text Search (FTS)比在左侧使用带有通配符的LIKE更简单,更快,因为LIKE左侧的通配符会使列上的任何索引都无法使用。但是,MySQL FTS要求搜索的表使用MyISAM引擎 - 查看Sphinx等第三方工具以实现相同的功能。

WHERE MATCH(column1, column2) AGAINST ($searchTerm)