gpt4 book ai didi

mysql - 选择所有相关产品

转载 作者:行者123 更新时间:2023-11-29 04:38:07 26 4
gpt4 key购买 nike

我有一个名为 teachings 的表:

id,name,description

还有一个 products 表:id,name,description,teachingID

我想从与产品相关联的教学中选择所有内容。如果有一个教学在产品表中没有产品,我不要它。

到目前为止我想出了:

SELECT * FROM `teachings`
LEFT JOIN `products` ON `products`.`teachingID` = `teachings`.`id`
WHERE COUNT(`products`) > 0

但这行不通:( 谁能给我指出正确的方向?

最佳答案

如果您只想返回至少与一种产品相关联的教学,请使用 exists 子查询。这样您就不必计算每次教学的产品数。

我的 mysql 生锈了,但这应该可以了

select *
from teachings t
where exists
(
select 1
from products p
where p.teachingID = t.id
)

关于mysql - 选择所有相关产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36449993/

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