好的,我已经使用了一段时间了,我有点卡住了。
也许我一切都错了!
基本上我有搜索字段的查询。一般的想法是选择基于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";
`
答案 0 :(得分: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;