对于我的网站,我创建了一个搜索页面,用户可以在其中搜索手机型号名称或制造商名称 - 但不能同时搜索两者。用户可以键入“apple”并获取所有Apple手机的列表,或者他们可以输入“3g”并获取名称中包含“3g”的所有手机的列表。
但是,如果用户键入“apple iphone”或“htc evo”,则结果为零,因为制造商名称和电话名称位于两个不同的列中。
这是我的搜索声明:
$sql = "SELECT
o.id,
o.label,
o.manufacture_id,
o.buyback_price,
o.image_path,
u.id,
u.label
FROM phone_models as o
LEFT JOIN manufactures as u ON o.manufacture_id = u.id
WHERE o.label like \"%$searchTerm%\" OR u.label like \"%$searchTerm%\"
order by o.label";
有没有办法创建一个包含u.label
和o.label
数据的新列?
谢谢!
答案 0 :(得分:4)
尝试:
$sql = "SELECT
o.id,
o.label,
o.manufacture_id,
o.buyback_price,
o.image_path,
u.id,
u.label
FROM phone_models as o
LEFT JOIN manufactures as u ON o.manufacture_id = u.id
WHERE CONCAT(u.label,' ',o.label) like \"%$searchTerm%\"
order by o.label";
但是用%:
替换$ searchTerm中的空格$searchTerm = str_replace(' ','%',$searchTerm);
答案 1 :(得分:0)
$sql = "SELECT
o.id,
o.label,
o.manufacture_id,
o.buyback_price,
o.image_path,
u.id,
u.label
FROM phone_models as o
LEFT JOIN manufactures as u
ON o.manufacture_id = u.id
WHERE CONCAT(o.label,' ',u.label)
LIKE REPLACE('%$searchTerm%', ' ', '%')
OR CONCAT(u.label,' ',o.label)
LIKE REPLACE('%$searchTerm%', ' ', '%')
ORDER BY o.label
";