0

如何通过具有多个搜索词的三个表使用 sql 和 php 进行良好的搜索?以及如何将其重新显示在屏幕上?

我在实习时自制的框架中工作。以下是我自己编造的,我认为这并不完全正确。

我已经准备好了以下想法:

function find_by_all($search) {
$searchwords = explode(" ", $search);

foreach ($searchwords as $word) {
    if ($word === end($searchwords)) {
        $query_where .= "companies.companyname LIKE  '%" . $word . "%' OR companies.city LIKE  '%" . $word . "%' OR persons.firstname LIKE  '%" . $word . "%' OR persons.lastname LIKE  '%" . $word . "%' OR investments.name LIKE  '%" . $word . "%' OR investments.firstname LIKE  '%" . $word . "%' OR investments.lastname LIKE  '%" . $word . "%'";
    } else {
        $query_where .= "companies.companyname LIKE  '%" . $word . "%' OR companies.city LIKE  '%" . $word . "%' OR persons.firstname LIKE  '%" . $word . "%' OR persons.lastname LIKE  '%" . $word . "%' OR investments.name LIKE  '%" . $word . "%' OR investments.firstname LIKE  '%" . $word . "%' OR investments.lastname LIKE  '%" . $word . "%' AND ";
     }
}

return $this->find_by_sql("SELECT * FROM companies LEFT JOIN investings ON investings.investing_id = companies.company_id LEFT JOIN investments ON investments.investment_id = companies.company_id LEFT JOIN persons ON persons.person_id = companies.company_id WHERE ".$query_where); 

现在我认为这相当不错,只有这个 SQL 和结果我还需要在我的屏幕上看到,只是我不知道该怎么做。谁能帮我?

4

1 回答 1

0

我做到了,现在不再需要它来帮助我。我已经对搜索功能进行了三个单独的查询,现在他做到了。

于 2013-06-18T07:19:29.983 回答