带有空列的 MySQL 内部连接

标签 mysql null inner-join

假设我有 2 个表

Table a
brand    Fruit         edible   color   size
farmers  banana        yes      yellow  null
fresh    banana        yes      red     10
bounty   banana        null     green   2
farmers  apple         yes      red     5
organic  grapes        null     violet  5
love     strawberry    yes      null    5
flow     lavander      no       null    null

Table b
boxId   fruit     edible    color   size
10100   banana     yes       yellow  9
19299   banana     yes       red     10
10992   apple      yes       red     5
10299   grapes     yes       red     5 
01929   lavander   no        violet  3

是否有可能连接表 a 和 b,即使有以下规则:如果有空值,则通过跳过空列继续评估剩余的列。

select a.brand, b.boxId from a prod
inner join b box on a.fruit = b.fruit
where a.edible = b.edible and 
a.color = b.color and 
a.size = b.size

brand       boxID
farmers      10100
fresh        19299
. . .

最佳答案

是这样的

select a.brand, b.boxId 
from a prod
inner join b box on a.fruit = b.fruit
where ( a.edible = b.edible OR a.edible IS NULL OR b.edible IS NULL ) and 
      ( a.color = b.color OR a.color IS NULL OR b.color IS NULL ) and 
      ( a.size = b.size OR a.size IS NULL OR b.size IS NULL)

关于带有空列的 MySQL 内部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42018600/

相关文章:

php - php内多层mysql调用

php - 对第三个关系表中的数量求和

python-3.x - 使用同一列中具有另一列中匹配值的值填充 Pandas DataFrame 中的空值

php - 帮助连接查询 (MySQL)

mysql - 如何使用 mysql 与表连接来编写复杂查询

php - 关于 PHP、Jquery 和 Ajax 的问题。

mysql - 年份更改时重置结果

php - 使用 PHP 检查 MySQL NULL 值

ios - 在尝试从 tableView 中分离出来时发现 nil

sql - INNER JOIN 的条件给出了错误的结果