Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
循环中的PHP/MySQL查询,如何只附加到一个查询?_Php_Mysql - Fatal编程技术网

循环中的PHP/MySQL查询,如何只附加到一个查询?

循环中的PHP/MySQL查询,如何只附加到一个查询?,php,mysql,Php,Mysql,我有一个从数据库中取出所有文章的查询 $get_articles = $db->query(" SELECT *, a.id AS id, s.type AS type FROM ".$prefix."articles a LEFT JOIN ".$prefix."sources s ON (s.id = source_id) WHERE a.type!='trashed' ORDER BY a.timestamp DESC LIMIT $start, $end

我有一个从数据库中取出所有文章的查询

$get_articles = $db->query("
    SELECT *, a.id AS id, s.type AS type FROM ".$prefix."articles a 
    LEFT JOIN ".$prefix."sources s ON (s.id = source_id) 
    WHERE a.type!='trashed' ORDER BY a.timestamp DESC LIMIT $start, $end");
在这个查询的循环中,我在同一个表上执行另一个查询,以查找与文章“title”相关的文章,存储为“$related\u phrase”。循环中的查询是:

// get related articles to this entry
$get_related = $db->query("
    SELECT *, a.id AS id, MATCH (title, description) AGAINST ('$related_phrase') AS score FROM ".$prefix."articles a 
    LEFT JOIN ".$prefix."sources s ON (s.id = source_id) WHERE a.type!='trashed' AND MATCH (title, description) AGAINST ('$related_phrase') AND a.id!='$articles[id]' HAVING score > 7 
    ORDER BY a.timestamp DESC LIMIT 0, 3");
这基本上意味着我们在循环中有一个查询,这会导致页面加载非常缓慢

我们要做的是,将查询从循环中带到主查询中,这样它就可以在一个查询中工作,如果可能的话


非常感谢任何帮助

我认为合并这两个查询不会加快速度

您可以尝试的一件事是获取所有文章和不同搜索词组的列表,例如,Attreable,然后构建一个查询,一次性获取所有相关文章。最后,将相关文章与文章列表进行匹配。

尝试以下方法:

$articles_and_related = $db->query("
    SELECT * 
    FROM ".$prefix."articles art 
    LEFT JOIN (
               SELECT * FROM ".$prefix."articles x
               WHERE 
                 score > 7 
                 AND x.type != 'trashed' 
                 AND x.id != art.id
                 AND MATCH(x.title, x.description) AGAINST (art.title)
               LIMIT 3
    ) rel 
    LEFT JOIN ".$prefix."sources s2 ON (s2.id = rel.source_id) 
    LEFT JOIN ".$prefix."sources s ON (s.id = art.source_id) 
    WHERE 
         art.type!='trashed' 
    ORDER BY art.timestamp DESC LIMIT $start, $end");