在metavalue中搜索,这是一个数组

时间:2012-03-20 12:04:28

标签: mysql wordpress

我尝试在wordpress上做一个个性化的搜索引擎。 所以我希望用户可以搜索所有高级自定义字段。我可以为只有一个值的简单字段执行此操作,但如果文本字段有多个以逗号分隔的值,则无法执行此操作。

对于我的查询,我有类似的东西:

SELECT DISTINCT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4 
WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
      wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND 
      wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
      lyon IN wp_postmeta.meta_value  AND wposts.post_status = 'publish' AND
      wposts.post_type = 'post' 
ORDER BY wposts.post_date DESC 

在具有多个值的字段内检查是否有事可做?

编辑:我尝试了FIND_IN_SET函数,但没有结果:

SELECT DISTINCT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4
WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
      wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND
      wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
      FIND_IN_SET('lyon', wpostmeta.meta_value) AND
      wposts.post_status = 'publish' AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC 

1 个答案:

答案 0 :(得分:0)

您应该使用LIKE或REGEXP来搜索字段 - 尝试:

SELECT DISTINCT wposts.*
FROM wp_posts wposts, wp_postmeta wpostmeta, wp_postmeta wpostmeta2, wp_postmeta wpostmeta3, wp_postmeta wpostmeta4
WHERE wposts.ID = wpostmeta.post_id AND wposts.ID = wpostmeta2.post_id AND
      wposts.ID = wpostmeta3.post_id AND wposts.ID = wpostmeta4.post_id AND
      wpostmeta.meta_key = 'ville_ou_se_deroulera_la_colonie' AND
      wpostmeta.meta_value REGEXP '^lyon[,]|[,]lyon[,]|[,]lyon$|^lyon$' AND
      wposts.post_status = 'publish' AND wposts.post_type = 'post'
ORDER BY wposts.post_date DESC

这应该找到以下任何一个

lyon
lyon,bordeaux
la rochelle,lyon
la rochelle,lyon,bordeaux

但不应该匹配:

la rochelle,lyons,bordeaux

即。只要“lyon”在其自身存在

,是否存在评论无关紧要