0

我正在创建一个在线系统,将银行对账单(描述、价值、类型等)与购买者(姓名、地址)相匹配,并希望对我目前这样做的方式提供一些反馈:

$array = array("keywords", "taken", "from", 
               "bank", "statements", "using", "explode", 
               "function", "using", "a", "space");

$i = 0;
$r = array(); //r = relevant

while($i<count($array)) {

  $keyword = $array[$i];

  $get = mysql_query("SELECT `id` FROM `users`, `addresses` 
                      LEFT JOIN `users`.`id` = `addresses`.`user` 
                      WHERE (`users`.`frame` LIKE '%$keyword%' 
                      OR `users`.`lname` LIKE '%$keyword%') 
                      OR ((`addresses`.`address` LIKE '%$keyword%' 
                      OR `addresses`.`town` LIKE '%$keyword%') 
                      OR (`addresses`.`county` LIKE '%$keyword%' 
                      OR `postcode`.`town` LIKE '%$keyword%'));");

  if(mysql_num_rows($get)) {
    while($fetch = mysql_fetch_array($get)) {
      list($var) = $fetch;
      push_array($r, $var);
    }
  }

  $i++;

}

//count the IDs that match within the relative array to see
//which is most relative to the search

有没有更好的方法来做到这一点以及将执行时间保持在最低限度?数据库完成后将达到数千个。

4

1 回答 1

1

最好建立一个将每个关键字与用户联系起来的关键字表,例如:

keywords (keyword, user) + UNIQUE(keyword, user)

keyword1   12
keyword1   14
keyword2   3

从要搜索的数据中填充关键字表后,查询变得更加优化:

SELECT users.* 
FROM keywords 
INNER JOIN users ON users.id = keywords.user
WHERE keyword IN ('keyword1', 'keyword2')

当然,当您对用户或地址表进行更改(插入、更新、删除)时,您需要维护此表。

于 2012-07-02T14:06:04.087 回答