我有一个MySQL Wordpress数据库,我正在处理postmeta表,它不是很好的结构。它有四列:" meta_id,post_id,meta_key,meta_value"。这是我正在使用的查询:
SELECT DISTINCT *
FROM wp_10_postmeta as pm
JOIN wp_10_posts AS p ON pm.post_id = p.ID
WHERE (meta_key LIKE 'personnel_%_last_name' OR
meta_key LIKE 'personnel_%_first_name' OR
meta_key LIKE 'personnel_%_title' OR
meta_key LIKE 'personnel_%_alias' OR
meta_key LIKE 'webpages_%_title' OR
meta_key LIKE 'webpages_%_alias' OR
meta_key LIKE 'departments_%_name' OR
meta_key LIKE 'departments_%_alias')
AND post_status = 'publish' AND meta_value LIKE '%john%' AND meta_value LIKE '%smith%'
问题:如果我搜索" john"此查询效果很好或者"史密斯"但是,如果我这样做,约翰史密斯"它返回空。有没有办法结合"人员_%_ last_name"和"人员_%_ first_name"然后比较?
答案 0 :(得分:0)
我猜你想要查看匹配的多个字段。尝试使用group by
来实现此目的:
SELECT p.*
FROM wp_10_postmeta as pm JOIN
wp_10_posts AS p
ON pm.post_id = p.ID
WHERE (meta_key LIKE 'personnel_%_last_name' OR
meta_key LIKE 'personnel_%_first_name' OR
meta_key LIKE 'personnel_%_title' OR
meta_key LIKE 'personnel_%_alias' OR
meta_key LIKE 'webpages_%_title' OR
meta_key LIKE 'webpages_%_alias' OR
meta_key LIKE 'departments_%_name' OR
meta_key LIKE 'departments_%_alias')
AND p.post_status = 'publish'
GROUP BY p.id
HAVING SUM(meta_value LIKE '%john%') > 0 AND SUM(meta_value LIKE '%smith%') > 0;