我有一個搜索查詢,用於在輸入(郵政編碼)周圍半徑爲x km的半徑內搜索工廠。它工作正常,但問題是我無法搜索輸入(郵政編碼),所以當我輸入9101時,例如它不搜索該郵政編碼,僅用於搜索郵政編碼。使用2次Mysql搜索並在同一字段上
我的查詢看起來是這樣的:
$string = implode($_SESSION['postcodes'], '|');
if (!isset($_COOKIE['cookie'])) {
$query = "SELECT * FROM (`bedrijfcategorieen`)
JOIN `bedrijven` ON `bedrijfcategorieen`.`idbedrijven` = `bedrijven`.`idbedrijven`
JOIN `categorieen` ON `bedrijfcategorieen`.`idcategorieen` = `categorieen`.`idcategorieen`
WHERE (`Bedrijfsnaam` LIKE '%".$this->input->post('search')."%'
OR `Plaats` LIKE '%".$this->input->post('search')."%'
OR `Telefoonnummer` LIKE '%".$this->input->post('search')."%'
OR `Email` LIKE '%".$this->input->post('search')."%'
OR `Website` LIKE '%".$this->input->post('search')."%'
OR `Profiel` LIKE '%".$this->input->post('search')."%'
OR `Adres` LIKE '%".$this->input->post('search')."%'
OR `Categorie` LIKE '%".$this->input->post('search')."%')
AND (Postcode REGEXP '$string')
GROUP BY `Categorie`, `bedrijfcategorieen`.`idbedrijven`";
$query = $this->db->query($query);
echo '<pre>';
echo '</pre>';
$result = $query->result_array();
return $result;
的$_SESSION['postcodes']
是在我的周圍郵政編碼一定的半徑所有郵政編碼會話。 我有另一個會話叫做searched_post_code
這個例子是爲了輸入'9101'。
如何搜索工廠填寫的郵政編碼和填寫的搜索詞?
我searchform看起來是這樣的:
注大「搜索」輸入半徑和郵政編碼輸入。
我也想匹配任何searchterms與我搜索的郵政編碼。
代碼形式:
<form name="input" method="post" action="searchresults" class="pro6pp_range">
<input type="search" onchange="validate()" placeholder="Zoeken..." name="search" size="70">
<select class="range">
<option value="5" selected="selected">5 km</option>
<option value="10">10 km</option>
<option value="15">15 km</option>
<option value="20">20 km</option>
<option value="25">25 km</option>
</select>
<input type="search" name="searchpc" class="postcode" value="<?= $this->input->cookie('postcode'); ?>" placeholder="Postcode (1234)" maxlength="4">
<input type="submit" value="Zoeken">
我希望這是一個有點清晰:
你可以看到它的工作SQL輸出的HERE
例子:
SELECT * FROM(
bedrijfcategorieen
)JOINbedrijven
ONbedrijfcategorieen
。idbedrijven
=bedrijven
。idbedrijven
JOINcategorieen
ONbedrijfcategorieen
。idcategorieen
=categorieen
。idcategorieen
WHERE(Bedrijfsnaam
LIKE '%設計%' ORPlaats
LIKE '%設計%' ORTelefoonnummer
LIKE '%設計%' ORWebsite
LIKE '%設計%' ORProfiel
LIKE 「%設計%'或Adres
LIKE'%design%'或Categorie
LIKE'%design%') AND(郵政編碼REGEXP '9100 | 9101 | 9121 | 9103 | 9148 | 9156 | 9146 | 9122 | 9155 | 9154 | 9147 | 9125 | 9144 | 9106 | 9138 | 9113 | 9104 | 9153 |') GROUP BYCategorie
,bedrijfcategorieen
。idbedrijven
如何生成的SQL的樣子? – hank
看我的編輯。我在codeigniter中直接使用它,所以不能給你一個更好的例子。 –
我在查詢中看不到任何聚合函數,那麼什麼是「GROUP BY」?另外,表別名? – Strawberry