php - WordPress 自定义 SQL-Query,太重了?

标签 php mysql sql wordpress inner-join

任何人都可以帮助我优化 WordPress 中的查询吗?

PHP代码

$serSlugSearchWord = seoUrl($post->post_title);
$serSlugSearchWord_obj = explode("-", $serSlugSearchWord);
foreach ($serSlugSearchWord_obj as $item )
{
    $queryStringKeyWords .= " if( INSTR(post_name, '$item')>0 ,1,0) +";
}

$queryStringKeyWords = preg_replace("/\+$/", "", $queryStringKeyWords);
$theQuery = "Select
$queryStringKeyWords 
as my_rate,
`wp_posts`.*  
from `wp_posts`
WHERE (`post_status` = 'publish' )
AND `post_type` = 'post'
order by my_rate desc limit 0, 10";
global $wpdb;
$wpdb->get_row($theQuery, 'ARRAY_N');

查询如下所示(并且效果很好)

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
order by my_rate desc limit 0, 10

现在我只想要类别 1 和 5 的帖子

INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
AND wp_term_relationships.term_taxonomy_id = 1
OR wp_term_relationships.term_taxonomy_id = 5

但是当我将此行添加到代码中时

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
AND wp_term_relationships.term_taxonomy_id = 1
OR wp_term_relationships.term_taxonomy_id = 5
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
order by my_rate desc limit 0, 10

它使我的网络空间(服务器)崩溃了。但为什么呢?

顺便说一句:此查询仅供管理员使用。

最佳答案

(已更新)你能尝试一下吗? OR 条件放在括号中。

Select if( INSTR(post_name, 'serengeti')>0 ,1,0) + if( INSTR(post_name, 'adventure')>0 ,1,0) as my_rate, `wp_posts`.* from `wp_posts` 
INNER JOIN wp_term_relationships
ON wp_term_relationships.object_id = ID
WHERE (`post_status` = 'publish' ) 
AND `post_type` = 'post' 
AND wp_term_relationships.term_taxonomy_id in (1,5)
order by my_rate desc limit 0, 10

关于php - WordPress 自定义 SQL-Query,太重了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26375665/

相关文章:

sql - mysql选择以 future 记录的存在为条件

c# - 如何从 mysql 数据库列在 C# 中的组合框中添加选项

mysql - 具有多对多和一对多关系的数据库表

PHP/MYSQL 添加按钮到列

php - Code-Golf:一行 PHP 语法

php - wp_title 过滤器对 <title> 标签没有影响

javascript - 使用 php 动态更改 html 表格单元格内容

php - 如何在 php 和 sql 中创建聊天对话列表?

sql - MySQL - 获取多个分组行的摘要?

javascript - Laravel 未定义