MYSQL INNER JOIN 可能为空字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17950351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:21:24  来源:igfitidea点击:

MYSQL INNER JOIN with possible empty fields

mysqljoincategories

提问by uollaa

Ok, I have a problem with joining 2 tables (with INNER JOIN). First table containts categories list (ecc. for photo albums, or blog posts) while the second table contains "the data"

好的,我在连接 2 个表时遇到问题(使用 INNER JOIN)。第一个表包含类别列表(例如相册或博客文章),而第二个表包含“数据”

I tried like this:

我试过这样:

SELECT galeries_id, galeries_title, 
       photos.photos_id, photos.photos_gal_id, photos.photos_link 
FROM galeries 
INNER JOIN photos 
ON galeries.galeries_id=photos.photos_gal_id 
GROUP BY photos_gal_id

This gives me fair result, joining my tables nicely, just as I want them to with one crucial exeption.

这给了我公平的结果,很好地加入了我的桌子,就像我希望他们通过一个关键的例外。

If table "photos" doesn't have a row which contains "photos_gal_id" (for example "2"), than it will NOT return any category (galeries_id, galeries_title) for that category even if it does egzist in galeries table.

如果表“photos”没有包含“photos_gal_id”的行(例如“2”),那么它不会返回该类别的任何类别(galeries_id,galeries_title),即使它在galeries表中执行egzist。

It is logical because of:

这是合乎逻辑的,因为:

 ON galeries.galeries_id=photos.photos_gal_id

Now I need to adopt this part to show me even thoes which doesn't have a related row in the second table

现在我需要采用这部分来向我展示即使在第二个表中没有相关行的那些

The result I'm trying to get is:

我想要得到的结果是:

galeries_id      galeries_title       photos_id       photos_link
     1               blabla              3            test.jpg
     2               bla bla2                                     
     3               etata               5            test.jpg

I hope I explained it good enough :) Thank you..

我希望我解释得足够好:)谢谢..

回答by Mark

To retain the rows from galerieswith no matching ID in photos, you will need to join photos on galeries with LEFT JOINinstead of INNER JOIN:

要保留galeries照片中没有匹配 ID的行,您需要将图库中的照片加入LEFT JOIN而不是INNER JOIN

SELECT galeries_id, galeries_title, 
       photos.photos_id, photos.photos_gal_id, photos.photos_link 
FROM galeries 
LEFT JOIN photos 
ON galeries.galeries_id=photos.photos_gal_id 
GROUP BY photos_gal_id

 

 

This will give you:

这会给你:

galeries_id      galeries_title       photos_id       photos_link
     1               blabla              3            test.jpg
     2               bla bla2            NULL         NULL                
     3               etata               5            test.jpg

 

 

And if you wish to replace NULL with an empty string, you can use:

如果你想用空字符串替换 NULL,你可以使用:

SELECT
  IFNULL(photos.photos_id, ''),
  IFNULL(photos.photos_link, '')

回答by Jacob Goulden

Wouldn't it just be a

它不会只是一个

LEFT JOIN photos

Because you want the records reguardless of wether or not they are filled?

因为你想让记录不管他们是否被填满?