嘿伙计我有一个查询,它目前从表单中获取关键字列表,并返回前5个匹配关联语句。它们位于两个不同的表格中(Keyword
& Problem
)。我遇到的问题是,如果有人搜索TURNS,则返回结果但如果搜索TURN则没有结果。我知道这看起来很简单,但我正在使用的查询不那么简单。这是我正在使用的查询:
SELECT p.*, pc.wordCount, pc.commonCount
FROM Problem p INNER JOIN (
SELECT p.Problem_Identity, COUNT(k.Keyword_Identity) AS "wordCount",
SUM(k.Keyword_Common) as "commonCount"
FROM( SELECT * FROM Keyword ) k
INNER JOIN KeywordsInProblem kip ON (k.Keyword_Identity = kip.Keyword_Identity)
INNER JOIN Problem p ON (p.Problem_Identity = kip.Problem_Identity)
WHERE UCASE(k.Keyword_Word) IN(\''. strtoupper(implode("', '", $keyArr)) .'\')
GROUP BY p.Problem_Identity) pc
ON (p.Problem_Identity = pc.Problem_Identity) where pc.commonCount > 0
ORDER BY pc.wordCount DESC, pc.commonCount DESC LIMIT 5
PS:$keyArr
变量是一个爆炸形式输入语句,以便在数据库中使用之前对其进行清理。
答案 0 :(得分:1)
$likeCondition = "";
foreach($keyArr as $key)
{
$likeCondition .= (strlen(trim($likeCondition)))?
" OR UCASE(k.Keyword_Word) LIKE '%".strtoupper($key)."%' ":
" WHERE UCASE(k.Keyword_Word) LIKE '%".strtoupper($key)."%' ";
}
$sql = "SELECT
p.*,
pc.wordCount,
pc.commonCount
FROM
Problem p
INNER JOIN (
SELECT
p.Problem_Identity,
COUNT(k.Keyword_Identity) AS wordCount,
SUM(k.Keyword_Common) AS commonCount
FROM(
SELECT * FROM Keyword) k
INNER JOIN
KeywordsInProblem kip
ON
(k.Keyword_Identity = kip.Keyword_Identity)
INNER JOIN
Problem p
ON
(p.Problem_Identity = kip.Problem_Identity)
".$likeCondition."
GROUP BY p.Problem_Identity) pc
ON
(p.Problem_Identity = pc.Problem_Identity)
WHERE
pc.commonCount > 0
ORDER BY pc.wordCount DESC, pc.commonCount DESC LIMIT 5";
答案 1 :(得分:0)
你必须遍历$ keyArr并在每次迭代时连接这个
OR UCASE(k.Keyword_Word) like \''.$keyArr[i] .'% \'
所有这些限制都包含在()
中