WordPress自定义SQL案例排序优先级

时间:2013-12-17 10:35:00

标签: php mysql sql wordpress

我希望有人可以帮助我。我正在尝试在WordPress中执行自定义SQL查询,并按预定义的优先级排序结果。

示例搜索字符串是“EU Directive”,查询需要查看帖子的标题和名为“intro”的自定义字段(文本字段)。

我需要通过在某个方面使用标题或简介匹配整个字符串来优先处理结果,然后在搜索字符串中只包含2个单词中的1个的结果之前进行排序。

查询有效,但排序却没有。仅在2个单词中的1个匹配的结果将在“intro”字段中具有完整字符串“EU Directive”的结果之前被拉出。

我认为下面的查询会正确设置排序,但不幸的是。

任何人都可以看到任何明显的错误吗?在此阶段,我们将非常感谢您的帮助。

SELECT * FROM wp_posts 

INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS     mt1 ON (wp_posts.ID = mt1.post_id) 
WHERE 1=1 AND ( ( 

(wp_posts.post_title LIKE 'EU Directive %') OR ((wp_postmeta.meta_key = 'intro' AND     CAST(wp_postmeta.meta_value AS CHAR) LIKE 'EU Directive %'))     ) 
OR ( (wp_posts.post_title LIKE 'EU Directive%') OR ((wp_postmeta.meta_key = 'intro' AND     CAST(wp_postmeta.meta_value AS CHAR) LIKE 'EU Directive%'))  ) 
OR ( (wp_posts.post_title LIKE '% EU Directive %') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive %'))    ) 
OR ( (wp_posts.post_title LIKE '% EU Directive,%') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive,%'))    ) 
OR ( (wp_posts.post_title LIKE '% EU Directive.%') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive.%'))    ) 
OR ( (wp_posts.post_title LIKE '% EU Directive%') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive%'))  ) 
OR ( (wp_posts.post_title LIKE 'EU %') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE 'EU %'))    ) 
OR ( (wp_posts.post_title LIKE '% EU %') OR ((wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU %'))    )
) 

AND wp_posts.post_type = 'documents' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID 

ORDER BY 

(CASE 

WHEN wp_posts.post_title LIKE 'EU Directive %' THEN 1 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE 'EU Directive %') THEN 2 
WHEN wp_posts.post_title LIKE 'EU Directive%' THEN 3 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE 'EU Directive%') THEN 4 
WHEN wp_posts.post_title LIKE '% EU Directive %' THEN 5 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive %') THEN 6 
WHEN wp_posts.post_title LIKE '% EU Directive,%' THEN 7 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive,%') THEN 8 
WHEN wp_posts.post_title LIKE '% EU Directive.%' THEN 9 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive.%') THEN 10 
WHEN wp_posts.post_title LIKE '% EU Directive%' THEN 11 
WHEN (wp_postmeta.meta_key = 'intro' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE '% EU Directive%') THEN 12
ELSE 13
END), 
wp_posts.post_date DESC LIMIT 0, 10

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您在CASE语句中尝试根据满足的条件输出排序值。问题是,从CASE语句中你得到一个数字:让我们说它是1.在这种情况下,你只是在做ORDER BY 1,这意味着“在第一列”。您应该将CASE用作SELECT中的字段,然后将字段名称放在ORDER BY子句中。像这样:

SELECT * FROM
    (SELECT *, (CASE WHEN... THEN ...) AS position FROM wp_posts ...) data
ORDER BY data.position, data.post_date DESC LIMIT 0, 10