梦想之翼
发布于

PHP MySql 搜索组合两列的结果

正在创建一个飞机列表网站,并尝试构建搜索功能。下面是运行良好的 MySql 搜索查询。然而;如果我搜索“cessna”,我会得到所有来自 的 Cessna,如果我搜索“172”,我会得到所有来自 的 172。但是如果我搜索 “Cessna 172”,我得到的结果为零。如果两者中都有匹配项,我将如何修改我的查询以查找 Cessna 和 172 将 the 和 search 组合在一起?提前感谢。listings_aircraft.makelistings_aircraft.modellistings_aircraft.makelistings_aircraft.model

  $query = "SELECT listing_control.*, 
   listings_aircraft.year, listings_aircraft.make, listings_aircraft.model, 
   listings_aircraft.price, listings_aircraft.callforprice, 
   listings_aircraft.about, listings_aircraft.aircraft_location
   FROM listing_control
   LEFT JOIN listings_aircraft
   ON listings_aircraft.id = listing_control.listing_id 
   WHERE listings_aircraft.make LIKE '%{$search}%'
   OR listings_aircraft.model LIKE '%{$search}%'
   OR listings_aircraft.about LIKE '%{$search}%' 
   OR listings_aircraft.registration LIKE '%{$search}%' 
   AND NOT listing_control.listing_status = 'draft' 
   AND NOT listing_control.listing_status = 'sold'
   AND NOT listing_control.listing_status = 'expired'
   ORDER BY listing_control.date_created DESC";
浏览 (21)
点赞
收藏
1条评论
Klustron小助手
Klustron小助手
试试这个 $search = trim($clean['search_input']); if (strlen($search) < 3) { die(); } $searchTerms = explode(' ', $search); $searchCondition = []; foreach ($searchTerms as $term) { $term = '%' . $term . '%'; $searchCondition[] = " (listings_aircraft.make LIKE '{$term}' OR listings_aircraft.model LIKE '{$term}' OR listings_aircraft.about LIKE '{$term}')"; } $searchCondition = implode(' AND ', $searchCondition); $query = "SELECT listing_control.*, listings_aircraft.year, listings_aircraft.make, listings_aircraft.model, listings_aircraft.price, listings_aircraft.callforprice, listings_aircraft.about, listings_aircraft.aircraft_location FROM listing_control LEFT JOIN listings_aircraft ON listings_aircraft.id = listing_control.listing_id WHERE ($searchCondition) AND NOT listing_control.listing_status = 'draft' AND NOT listing_control.listing_status = 'sold' AND NOT listing_control.listing_status = 'expired' ORDER BY listing_control.date_created DESC";
点赞
评论