搜索连接表仅显示来自一个表的匹配数据

时间:2016-06-28 17:16:07

标签: mysql search join

我尝试使用以下查询执行搜索:

        SELECT 
            *
        FROM 
            question_index
        JOIN
            question_content
            ON question_index.id = question_content.question_id
        WHERE
            question_index.remove = '0' AND
            question_index.active = '1' AND         
            question_index.publish_date <= '$current_time' AND
            (
                question_index.question LIKE '%$keyword%' OR
                question_content.text LIKE '%$keyword%'
            )
        GROUP BY
            question_index.id
        ORDER BY 
            question_index.publish_date DESC

我想要做的是搜索在两个表中运行并显示与&#34; question_index.question&#34;匹配的结果。或&#34; question_content.text&#34;

但是现在,它只显示符合&#34; question_content.text&#34;的结果。我尝试删除&#34; question_content.text LIKE&#39;%$ keyword%&#39;&#34;但结果根本没有出现。唯一可以获得匹配&#34; question_index.question&#34;如果我一起删除连接。但那不是我想要的结果。

1 个答案:

答案 0 :(得分:0)

  SELECT 
        *
    FROM 
        question_index LEFT
    JOIN
        question_content
        ON question_index.id = question_content.question_id
    WHERE
        question_index.remove = '0' AND
        question_index.active = '1' AND         
        question_index.publish_date <= '$current_time' AND
        (
            question_index.question LIKE '%$keyword%' OR
            question_content.text LIKE '%$keyword%'
        )
    GROUP BY
        question_index.id
    ORDER BY 
        question_index.publish_date DESC