2

好的,我已经使用这个有一段时间了,我有点卡住了。

也许我做错了!

基本上我有一个搜索字段的查询。总体思路是根据 LIKE %% 选择结果,同时仍将完全匹配放在首位。

因此,例如,如果您搜索 47,我希望显示 id、surname 或 company_name 中带有 47 的所有内容,但是如果我输入姓氏,则 id 编号为 47 的结果应该在顶部并且相同。

请参阅下面的代码,它可能有助于澄清我的问题。

SELECT id, 
IF(company_name IS NOT NULL AND company_name <> '', company_name, surname) AS name, 
first_name, country, phone1, isowner, isholidayrenter, isproholidayrenter, 
islongtermrenter, isprolongtermrenter, isprobuyer, isbuyer 
FROM clients 
WHERE id LIKE '$search' OR surname LIKE '$search' OR company_name LIKE '$search'
union all
SELECT id, 
IF(company_name IS NOT NULL AND company_name <> '', company_name, surname) AS name,      
first_name, country, phone1, isowner, isholidayrenter, isproholidayrenter,     
islongtermrenter, isprolongtermrenter, isprobuyer, isbuyer 
FROM clients 
WHERE id LIKE '%$search%' AND id NOT LIKE '$search' OR surname LIKE '%$search%' 
and SURNAME NOT LIKE '$search' OR company_name LIKE '%$search%' 
and company_name NOT LIKE '$search' 
LIMIT $start, $limit";

`

4

1 回答 1

5

尝试这个:

(选择完全匹配)将所有与(选择部分匹配省略完全匹配)联合

示例

(
  SELECT
      id, 
      IF( company_name IS NOT NULL AND company_name <> '', company_name, surname ) AS name,
      first_name, country, phone1, isowner, isholidayrenter, isproholidayrenter, 
      islongtermrenter, isprolongtermrenter, isprobuyer, isbuyer 
  FROM
      clients 
  WHERE
      id LIKE '$search' OR 
      surname LIKE '$search' OR 
      company_name LIKE '$search'
)
union all
(
  SELECT 
      id,
      IF( company_name IS NOT NULL AND company_name <> '', company_name, surname ) AS name, 
      first_name, country, phone1, isowner, isholidayrenter, isproholidayrenter,
      islongtermrenter, isprolongtermrenter, isprobuyer, isbuyer 
  FROM
      clients 
  WHERE
      ( id LIKE '%$search%' AND id NOT LIKE '$search' ) OR 
      ( surname LIKE '%$search%' AND SURNAME NOT LIKE '$search' ) OR
      ( company_name LIKE '%$search%' AND company_name NOT LIKE '$search' )
)
LIMIT $start, $limit;
于 2012-06-21T12:40:37.363 回答