这就是我要做的事情。我有一个'标签列表'在一个领域内。我试图让用户使用这些标签深入挖掘搜索结果。这是该领域的样子:
假设用户想要搜索10 fr,男性,间歇性导管。我不能像%那样使用%,因为有些标签是相似的(例如:男性,女性)。有没有办法将其分解为数组并像在php中一样进行搜索?或类似的东西?
理想情况下,我想分隔每个关键字,并将每个标记与列表进行比较,以确保它们全部在那里。
我能提出的最接近的是:
Select d.field_id_115 as tags,tag.tag_name, title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE cat_title.cat_url_title = 'catheter-supplies'
AND tag.tag_name ='male'
AND d.field_id_115 LIKE '%silicone%';
或者,每个标记也可以拥有它自己的字段,但我不确定如何检查以确保如果有一个或多个标记,则包含两个标记。
因此,我需要确保entry_id具有每个标签选择的记录。我尝试用以下方法做到这一点:
SELECT entry_id
FROM (Select title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE tag.tag_name IN ('silicone')
AND cat_title.cat_url_title = 'catheter-supplies'
UNION ALL
Select title.title, title.entry_id, cat_title.cat_name FROM finn_tagger tag LEFT JOIN finn_tagger_links link
ON tag.tag_id = link.tag_id
LEFT JOIN finn_channel_titles title
ON link.entry_id = title.entry_id
LEFT JOIN finn_category_posts cat
ON title.entry_id = cat.entry_id
LEFT JOIN finn_categories cat_title
ON cat.cat_id = cat_title.cat_id
Left JOIN finn_channel_data d
ON cat.entry_id = d.entry_id
WHERE tag.tag_name IN ('male')
AND cat_title.cat_url_title = 'catheter-supplies') as e
group by entry_id HAVING COUNT(*) > 1
但这似乎并不是最好的方式,而且速度很慢。
答案 0 :(得分:0)
首先,您将选择要匹配的逗号分隔数据库字段,如下所示:
SELECT REPLACE(comma_separated_field, ',', ' ') FROM some_table
接下来,为了细分这些词,您需要使用explode()
,如下所示:
$search_term = '10 fr, male, intermittent catheter';
$search_term = explode(',', $search_term);
最后,您希望将类似的单词与like语句匹配,请注意%
符号之间的空格。此方法将成功搜索您想要的多个完全匹配:
$like = '';
for($q=0; $q < count($search_term); $q++){
if($q+1 != count($search_term)){
$like .= "db_field LIKE '% " . $search_term[$q] . " %' AND ";
}else{
$like .= "db_field LIKE '% " . $search_term[$q] . " %'";
}
}