mysql 左连接 in_MySQL 使用左连接替换not in

众所周知,左连接和右连接的含义是以哪一张表为准。

左连接就是以左表为准,查出的结果中包含左表所有的记录,如果右表中没有与其对应的记录,那么那一行记录中B表部分的内容就全是NULL。

141e852f283a6ea20bc7265f599f37b0.png

现在有两个表,一个category表和goods表:

mysql> select * from category;

+----+---------+

| id | cate |

+----+---------+

| 1 | food |

| 2 | clothes |

| 3 | book |

| 4 | sport |

| 5 | music |

| 6 | video |

+----+---------+

6 rows in set (0.01 sec)

mysql> select * from goods;

+----+---------+-------------------+

| id | cate_id | name |

+----+---------+-------------------+

| 0 | 5 | You Are Not Alone |

| 1 | 2 | T-shirt |

| 2 | 1 | water |

| 3 | 1 | rice |

| 4 | 3 | C++ primer |

| 5 | 4 | basketbal |

+----+---------+-------------------+

6 rows in set (0.00 sec)

现在要查出每一种分类下的商品,那么可以很简单的使用左连接了:

mysql> select * from category

-> left join goods

-> on category.id = goods.cate_id

-> order by category.id;

+----+---------+------+---------+-------------------+

| id | cate | id | cate_id | name |

+----+---------+------+---------+-------------------+

| 1 | food | 2 | 1 | water |

| 1 | food | 3 | 1 | rice |

| 2 | clothes | 1 | 2 | T-shirt |

| 3 | book | 4 | 3 | C++ primer |

| 4 | sport | 5 | 4 | basketbal |

| 5 | music | 0 | 5 | You Are Not Alone |

| 6 | video | NULL | NULL | NULL |

+----+---------+------+---------+-------------------+

7 rows in set (0.01 sec)

从上面的结果中很全就能看到video分类中没有商品。

需求:只查询哪一种分类下面没有商品

这个很好实现,可以用下面几个方法:

1、使用not in

mysql> select * from category where id not in ( select cate_id from goods);

+----+-------+

| id | cate |

+----+-------+

| 6 | video |

+----+-------+

1 row in set (0.01 sec)

2、仍旧使用左连接,只不过对于结果加一个where筛选

mysql> select * from category

-> left join goods

-> on category.id = goods.cate_id

-> where goods.id is NULL;

+----+-------+------+---------+------+

| id | cate | id | cate_id | name |

+----+-------+------+---------+------+

| 6 | video | NULL | NULL | NULL |

+----+-------+------+---------+------+

1 row in set (0.00 sec)

至于为什么不适用not in,这是因为他不使用索引,如果数据量大的时候,效率并不高。

同样,如果要显示和总表中匹配了记录,隐藏左表没有匹配到的记录,可以将后面的where goods.id is not NULL;

同样,对于右连接来说也是一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值