这是两个非常相似的查询。它们之间的区别在于,在第二个查询中,我添加了一个名为ng2s3_map的新表,我只需要一个列。问题是第一个查询根据需要一直工作,但没有输出lik列,第二个查询只有在表ng2s3_map中有行时才有效,否则不输出任何内容。如何更改第二个查询,因此无论ng2s3_map表中是否存在行,它都会输出结果。 (我添加2个表的唯一原因是为了更好地解释我的问题)。
首先查询:
SELECT ng2s3_content.ID, ng2s3_similar.similar_ID, ng2s3_similar.up, ng2s3_similar.down, similar_articles.Title AS SimilarTitle, similar_articles.image AS image
FROM ng2s3_content
JOIN ng2s3_similar ON ng2s3_content.ID = ng2s3_similar.ID
JOIN ng2s3_content similar_articles ON ng2s3_similar.Similar_ID = similar_articles.ID
WHERE ng2s3_content.ID =4764
第二次查询:
SELECT ng2s3_content.ID, ng2s3_similar.similar_ID, ng2s3_similar.up, ng2s3_similar.down, ng2s3_map.similar_id, ng2s3_map.vote_id, ng2s3_map.lik, similar_articles.Title AS SimilarTitle, similar_articles.image AS image
FROM ng2s3_content
JOIN ng2s3_similar ON ng2s3_content.ID = ng2s3_similar.ID
JOIN ng2s3_content similar_articles ON ng2s3_similar.Similar_ID = similar_articles.ID
JOIN ng2s3_map ON ng2s3_map.similar_id = ng2s3_similar.similar_ID
AND ng2s3_map.vote_id = ng2s3_content.ID
WHERE ng2s3_content.ID =4764
答案 0 :(得分:1)
如果是这种情况,那么您需要使用LEFT JOIN
代替INNER JOIN
SELECT ng2s3_content.ID,
ng2s3_similar.similar_ID,
ng2s3_similar.up,
ng2s3_similar.down,
ng2s3_map.similar_id,
ng2s3_map.vote_id,
ng2s3_map.lik,
similar_articles.Title AS SimilarTitle,
similar_articles.image AS image
FROM ng2s3_content
INNER JOIN ng2s3_similar
ON ng2s3_content.ID = ng2s3_similar.ID
INNER JOIN ng2s3_content similar_articles
ON ng2s3_similar.Similar_ID = similar_articles.ID
LEFT JOIN ng2s3_map
ON ng2s3_map.similar_id = ng2s3_similar.similar_ID AND
ng2s3_map.vote_id = ng2s3_content.ID
WHERE ng2s3_content.ID = 4764
要进一步了解联接,请访问以下链接: