INNER JOIN mysql布尔错误

时间:2011-06-30 21:18:27

标签: php mysql sql

$results2=mysql_query("
SELECT * FROM searchengine
WHERE
   id IN (" . implode(',', $ids) . ")
   OR id IN (" . implode(',', $ids2) . ")
INNER JOIN keywords ON searchengine.id=keywords.id
ORDER BY
   (relevant-irrelevant) DESC,
   (rating/votes) DESC,
   report ASC,
   LENGTH(description) DESC,
   title ASC
LIMIT $page, $limit
");

上面代码中的某些功能不像我想的那样, While 循环返回布尔错误。

内爆函数的代码工作正常。

我的数据库是 searchengine 关键字

searchengine:id,title,desc ...

关键字:num,id,a,b

来自id的A和B应该添加到searchengine(基于相同的id)来制作类似(id,title,desc,a,b ......)的东西。问我需要更多细节。

注意:searchengine id是唯一编号,但关键字可以多次具有相同的ID(其中一个相同的ID由A和B值选取并插入为$ ids1)。

2 个答案:

答案 0 :(得分:3)

  • 移动连接下方的where子句。
  • 使用表名限定id。 searchengine.id

SELECT *
FROM searchengine
INNER JOIN keywords ON searchengine.id=keywords.id
WHERE
   searchengine.id IN (" . implode(',', $ids) . ")
   OR searchengine.id IN (" . implode(',', $ids2) . ")
ORDER BY
   (relevant-irrelevant) DESC,
   (rating/votes) DESC,
   report ASC,
   LENGTH(description) DESC,
   title ASC
LIMIT $page, $limit

答案 1 :(得分:0)

您的WHERE子句应该在JOIN之后。

SELECT * 
    FROM searchengine 
        INNER JOIN keywords 
            ON searchengine.id=keywords.id 
    WHERE id IN (" . implode(',', $ids) . ") 
       OR id IN (" . implode(',', $ids2) . ")
    ORDER BY (relevant-irrelevant) DESC, (rating/votes) DESC, report ASC,  LENGTH(description) DESC, title ASC 
    LIMIT $page, $limit