Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.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 返回错误的大型sql查询(mysql)_Php_Mysql_Concat - Fatal编程技术网

Php 返回错误的大型sql查询(mysql)

Php 返回错误的大型sql查询(mysql),php,mysql,concat,Php,Mysql,Concat,我目前正在尝试更新我的内部搜索引擎以使用多个单词。这是非常大和复杂,但我遇到了错误,我从来没有遇到过,现在我不知道为什么 所以问题是,为什么我会收到下面的错误 为了更好地理解,我将把它分成不同的部分 这就是我复制并粘贴到PHPMyAdmin中的SQL查询(PHPMyAdmin很好地格式化了它),它在数据库中搜索2个单词: SELECT * FROM ( SELECT p.page_url AS url, COUNT( * ) AS occurrences FROM PAGE p, word w

我目前正在尝试更新我的内部搜索引擎以使用多个单词。这是非常大和复杂,但我遇到了错误,我从来没有遇到过,现在我不知道为什么

所以问题是,为什么我会收到下面的错误

为了更好地理解,我将把它分成不同的部分

这就是我复制并粘贴到PHPMyAdmin中的SQL查询(PHPMyAdmin很好地格式化了它),它在数据库中搜索2个单词:

SELECT *
FROM (

SELECT p.page_url AS url, COUNT( * ) AS occurrences
FROM PAGE p, word w, occurrence o
WHERE (
(
p.page_id = o.page_id
AND w.page_word_id = o.page_word_id
AND w.word_word LIKE '%' 'test' '%'
GROUP BY p.page_id
)
OR (
p.page_id = o.page_id
AND w.page_word_id = o.page_word_id
AND w.word_word LIKE '%' 'search' '%'
GROUP BY p.page_id
)
UNION (

SELECT f.file_url AS url, COUNT( * ) AS occurrences
FROM files f, filenames fn, fileoccurrence fo
WHERE f.file_id = fo.file_id
AND fn.file_word_id = fo.file_word_id
AND fn.file_word LIKE '%' 'test' '%'
GROUP BY f.file_id
)
OR (

SELECT f.file_url AS url, COUNT( * ) AS occurrences
FROM files f, filenames fn, fileoccurrence fo
WHERE f.file_id = fo.file_id
AND fn.file_word_id = fo.file_word_id
AND fn.file_word LIKE '%' 'search' '%'
GROUP BY f.file_id
)
)t
ORDER BY occurrences DESC 
下面的PHP代码使用搜索输入中的explode函数生成此代码

// Do a little formatting
$keyword = strtolower($keyword);

// Get timestamp for start
$start_time = microtime(true);

$searched_words = explode(' ', $keyword);

foreach ($searched_words as $index => $word) {
    // Set up the stemmer
    $stemmer = new PorterStemmer;
    $stemmed_string = $stemmer->stem($word);
    $searched_words[$index] = $stemmed_string;
}

//  Configure the sql code
$sql = "SELECT * FROM (SELECT p.page_url AS url, COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o WHERE (";

// Add the extra words to the sql
foreach ($searched_words as $index => $word) {  
    $sql .= "(p.page_id = o.page_id AND w.page_word_id = o.page_word_id
        AND w.word_word LIKE '%' '" . $word . "' '%' GROUP BY p.page_id) OR";
}
// Add the union to the sql and then add the second query   
$sql = substr($sql, 0, (strLen($sql)-3)); //this will eat the last OR
$sql .= " UNION ";

// The second set of querys
foreach ($searched_words as $index => $word) {
    $sql .= "(SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo
        WHERE f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word
        LIKE '%' '" . $word . "' '%' GROUP BY f.file_id) OR";
}

// Clsoe the sql code
$sql = substr($sql, 0, (strLen($sql)-3)); //this will eat the last OR
$sql .= ") t ORDER BY occurrences DESC"; // LIMIT " . $results . "");

// echo the query for the pure lolz of it
echo $sql . "<br /><br />";

// Search the DB for the results
$results = mysql_query($sql)
    or die("Invalid query: " . mysql_error());

编辑:修复了上述错误。使用此代码

//  Configure the sql code
$sql = "SELECT * FROM (SELECT p.page_url AS url, COUNT(*) AS occurrences 
    FROM page p, word w, occurrence o WHERE (";

// Add the extra words to the sql
foreach ($searched_words as $index => $word) {  
    $sql .= "(p.page_id = o.page_id AND w.page_word_id = o.page_word_id
        AND w.word_word LIKE CONCAT('%', '" . $word . "', '%'))) OR "; //GROUP BY p.page_id)
}
// Add the union to the sql and then add the second query   
$sql = substr($sql, 0, (strLen($sql)-4)); //this will eat the last OR
$sql .= " GROUP BY p.page_id)";
$sql .= " UNION ";
$sql .= "(SELECT f.file_url AS url, COUNT(*) AS occurrences FROM files f, filenames fn, fileoccurrence fo
    WHERE (";

// The second set of querys
foreach ($searched_words as $index => $word) {
    $sql .= "(f.file_id = fo.file_id AND fn.file_word_id = fo.file_word_id AND fn.file_word
        LIKE CONCAT('%', '" . $word . "', '%'))) OR "; //GROUP BY f.file_id)
}

// Clsoe the sql code
$sql = substr($sql, 0, (strLen($sql)-4)); //this will eat the last OR
$sql .= " GROUP BY f.file_id)";
$sql .= ") t ORDER BY occurrences DESC"; // LIMIT " . $results . "");
这现在会产生错误:

无效查询:每个派生表都必须有自己的别名


这与联合有关(我不太擅长联合..或一般的SQL)

在SQL中,不能通过将字符串相邻放置来连接字符串

AND w.word_word LIKE '%' 'test' '%'
应该是

AND w.word_word LIKE CONCAT('%', 'test', '%')
或者,如果使用
将SQL\u MODE='PIPES\u设置为\u CONCAT'
来获取标准的ANSI SQL语法,则可以使用:

AND w.word_word LIKE '%' || 'test' || '%'

根据您的评论,我发现了另一个问题:

在SQL中,必须先完成WHERE子句,然后才能添加GROUPBY子句。语法是:

WHERE ( <conditions...> )
GROUP BY <expressions>

如果将
省略为x
,则这是一个错误(在本例中,x只是一个示例,您可以选择一个更有意义的别名)。

在SQL中,不能通过将字符串相邻放置来连接字符串

AND w.word_word LIKE '%' 'test' '%'
应该是

AND w.word_word LIKE CONCAT('%', 'test', '%')
或者,如果使用
将SQL\u MODE='PIPES\u设置为\u CONCAT'
来获取标准的ANSI SQL语法,则可以使用:

AND w.word_word LIKE '%' || 'test' || '%'

根据您的评论,我发现了另一个问题:

在SQL中,必须先完成WHERE子句,然后才能添加GROUPBY子句。语法是:

WHERE ( <conditions...> )
GROUP BY <expressions>

如果将
省略为x,则这是一个错误(在本例中,x只是一个示例,您可以选择一个更有意义的别名)。

在生成的SQL中,括号不匹配。。。有9’(‘和8’)个


您是否也注释掉了
限制“$results.”;
作为上次代码更改的一部分?

在生成的SQL中,括号不匹配…有9'('s和8')

您是否也注释掉了
LIMIT”$后果"");作为上次代码更改的一部分