Mysql 从表中获取组合数据

Mysql 从表中获取组合数据,mysql,Mysql,我有个愚蠢的问题,好几天没解决了 我有三张桌子 表“分销商” 表“产品”: 表“DistributorProduct”-哪个分销商可以交付哪个产品 ID | DistributorID | ProductID 1 | 1 | 1 2 | 1 | 2 3 | 1 | 3 4 | 1 | 4 5 | 2 | 1 6 | 2 | 3 7 | 2 | 4 7 | 3 | 3 7 | 4 | 4 现在我想得到所有能提供苹果和香蕉的分销商。(结果必须是分配器A和分配器B) 我该怎么做?什么是SQL语句 非

我有个愚蠢的问题,好几天没解决了

我有三张桌子

表“分销商”

表“产品”:

表“DistributorProduct”-哪个分销商可以交付哪个产品

ID | DistributorID | ProductID
1 | 1 | 1
2 | 1 | 2
3 | 1 | 3
4 | 1 | 4
5 | 2 | 1
6 | 2 | 3
7 | 2 | 4
7 | 3 | 3
7 | 4 | 4
现在我想得到所有能提供苹果和香蕉的分销商。(结果必须是分配器A和分配器B)

我该怎么做?什么是SQL语句

非常感谢你的帮助

迪特尔

select d.* 
from Distributor d
left join DistributorProducts dp on dp.DistributorId = d.id
left join Product p on p.Id = dp.ProductId and p.product = 'Apple' and     
p.product = 'Banana'
如果某个联接表达式出错,则可以不使用联接

select d.*
from Distributor d, DistributorProducts dp, Product p
where dp.DistributorId = d.id
and p.Id = dp.ProductId
and (p.product = 'Apple' AND p.product = 'Banana');
我相信这应该行得通。

这应该行得通:

select Distributor.ID, Distributor.Name
from Distributor, Product Apple, Product Banana, DistributorProduct
where
    DistributorProduct.DistributorID = Distributor.ID and
    DistributorProduct.ProductID = Apple.ID and
    Apple.Name = 'Apple' and
    DistributorProduct.ProductID = Banana.ID and
    Banana.Name = 'Banana'
group by Distributor.ID

欢迎来到堆栈溢出!请出示你的问题。您应该至少包括您遇到问题的代码的大纲(但最好是a),然后我们可以尝试帮助解决特定问题。你也应该读书,那不行。错误消息:“不支持联接表达式”@derdieter:已用其他解决方案更新。检查它。在你的解决方案中,我得到一个空结果。问题似乎是第二个“和”。如果没有“和p.product='Banana';”我用“Apple”得到所有结果。但同时使用AND(“苹果”和“香蕉”)没有结果。是的,我得到了。现在检查这给了我所有卖苹果的经销商和所有卖香蕉的经销商。但我只需要能同时销售这两种产品的分销商。你们对“苹果产品,香蕉产品”的定义是什么?什么是Apple.Name?它是“产品即苹果”AppleSo Apple的缩写别名。Name是Product.Name,我在这里使用了两个产品副本,一个按Name='Apple'过滤,另一个按Name='Banana'过滤
select d.*
from Distributor d, DistributorProducts dp, Product p
where dp.DistributorId = d.id
and p.Id = dp.ProductId
and (p.product = 'Apple' AND p.product = 'Banana');
select Distributor.ID, Distributor.Name
from Distributor, Product Apple, Product Banana, DistributorProduct
where
    DistributorProduct.DistributorID = Distributor.ID and
    DistributorProduct.ProductID = Apple.ID and
    Apple.Name = 'Apple' and
    DistributorProduct.ProductID = Banana.ID and
    Banana.Name = 'Banana'
group by Distributor.ID