我目前正在尝试更新我的内部搜索引擎以使用多个单词。这是非常庞大和复杂但我遇到了错误,我现在从未遇到过错误,我不明白为什么。
所以问题是,为什么我收到以下错误?
我将把它分成不同的部分以便更好地理解。
这只是我echo
的SQL查询,复制并粘贴到PHPMyAdmin,粘贴在这里(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());
所有这些都会返回错误:
查询无效:您的SQL语法出错;检查手册 对应于您的MySQL服务器版本,以获得正确的语法 在'GROUP BY p.page_id)附近使用或(p.page_id = o.page_id AND 第3行的w.page_word_id = o.page_word_id'
为什么我收到此错误?我之前使用的代码工作得很好。我添加的唯一真实内容是foreach()
。
这是我原来的SQL代码:
$result = mysql_query("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 '%' '" . $stemmed_string . "' '%' 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 '%' '" . $stemmed_string . "' '%' GROUP BY f.file_id) t ORDER BY occurrences DESC") // LIMIT " . $results . "")
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)
答案 0 :(得分:4)
只能将SQL中的字符串放在一起,就不能在SQL中连接它们。
AND w.word_word LIKE '%' 'test' '%'
应该是
AND w.word_word LIKE CONCAT('%', 'test', '%')
或者,如果使用SET SQL_MODE='PIPES_AS_CONCAT'
获取标准ANSI SQL语法,则可以使用:
AND w.word_word LIKE '%' || 'test' || '%'
重新评论,我看到另一个问题:
在SQL中,必须先完成WHERE子句,然后才能添加GROUP BY子句。语法是:
WHERE ( <conditions...> )
GROUP BY <expressions>
你有:
WHERE ( <conditions...> GROUP BY <expressions> )
OR ( <conditions...> GROUP BY <expressions> )
你拥有的不是合法的语法。
真的,这是你应该能够在SQL的任何初学者参考资料中查找的东西。
每个派生表都必须有自己的别名
这意味着您在FROM子句中使用了子查询但没有给它别名。例如:
SELECT ... FROM (SELECT ... FROM table) AS x WHERE ...etc...
如果省略AS x
,则表示错误(在这种情况下,x只是一个示例,您可以选择更有意义的别名)。
答案 1 :(得分:0)
在生成的SQL中,括号不匹配...有9'('s和8')。
您是否也将LIMIT " . $results . "");
评论为上次代码更改的一部分?