gpt4 book ai didi

mysql - 如何从 MySQL 中的连接表中获取孤儿

转载 作者:行者123 更新时间:2023-11-29 01:16:57 29 4
gpt4 key购买 nike

假设有 2 个表,第一个是产品(产品)列表,第二个是产品和另一个表(类别)之间的连接表,称为产品类别

产品:

id   |  name
------------
1 Lorem
2 Ipsum
3 Dolor
4 Sit

产品类别

product_id  | categories_id
---------------------------
1 3
1 6
4 1
2 2

如何获取孤立元素,我的意思是没有类别的元素,所以在这种情况下:3,使用 MyISAM 以有效的方式(+30k 记录)?

这有点像显示所有不可连接的行,但这种语法对我来说很奇怪......

最佳答案

select * from products p 
left join product_categories pc on p.id=pc.product_id
where pc.product_id is null

将返回 products 表中未在 product_Category 中找到的所有产品。 LEft join 和 where 非常快。 30k条记录也很少,所以不用担心。

关于mysql - 如何从 MySQL 中的连接表中获取孤儿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19241817/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com