我有5個表我想從中選擇數據,但有時第5個表(名爲comment)將是空的。當發生這種情況時,我希望我的查詢爲該表中的值返回null,並且只返回其他值(第5個表包含用戶註釋,有時不存在)。mysql SELECT column only if if exists
這裏是我的查詢:
SELECT articles.title, articles.posted, articles.body, authors.name, authors.img, authors.bio, comment.user_id, comment.text, comment.article_id, GROUP_CONCAT(categories.cat_name) AS cat_name
FROM articles, authors, categories, article_categories, comment
WHERE articles.author_id = authors.id
AND articles.id = article_categories.article_id
AND article_categories.category_id = categories.id
AND articles.id = comment.article_id
AND title LIKE :title;
的:title
來自PDO,而不是與此有關。
問題出在AND articles.id = comment.article_id
。我不知道怎麼寫,只是爲了檢查這個,如果它在那裏,否則就忽略它。
感謝您的幫助
使用連接(特別是'左JOIN') – h2ooooooo