MySQL连接到同一个表

MySQL连接到同一个表,mysql,join,self-join,Mysql,Join,Self Join,我有一个表(称为staging),其中包含以下相关字段: id (PRIMARY) bundle_id (INT) product (enum H,L,D) bundle_code (enum 10,20) 我需要搜索bundle_id,其中bundle_代码为10,然后还需要检索具有相同bundle_id的任何其他记录,其中product=H,以及最多两个具有相同bundle_id的其他记录,其中product!=H.我试图在一个查询中完成这一切,每个bundle\u id返回一行;所以我有

我有一个表(称为staging),其中包含以下相关字段:

id (PRIMARY)
bundle_id (INT)
product (enum H,L,D)
bundle_code (enum 10,20)
我需要搜索bundle_id,其中bundle_代码为10,然后还需要检索具有相同bundle_id的任何其他记录,其中product=H,以及最多两个具有相同bundle_id的其他记录,其中product!=H.我试图在一个查询中完成这一切,每个bundle\u id返回一行;所以我有一个bundle\u id的列表,每个都包含附加到该bundle\u id的每个产品和id

我想到的最好的办法是:

SELECT e1.bundle_id AS b_id, e1.product AS prod, e1.id AS id, 
e2.bundle_id AS b2_id, e2.product AS prod2, e2.id AS id2,
e3.bundle_id AS b3_id, e3.product AS prod3, e3.id AS id3,
e4.bundle_id AS b4_id, e4.product AS prod4, e4.id AS id4,    
FROM `staging` AS e1
INNER JOIN `staging` AS e2 ON (e1.bundle_id = e2.bundle_id AND e1.id != e2.id)
INNER JOIN `staging` AS e3 ON (e2.bundle_id = e3.bundle_id AND e2.id != e3.id)
INNER JOIN `staging` AS e4 ON (e1.bundle_id = e4.bundle_id AND e3.id != e4.id)
WHERE e1.bundle_code = '10'
AND e2.bundle_code = '20'
AND e2.product = 'H'
AND e3.product != 'H'
AND e4.product != 'H'
如果总共有四个结果,这似乎可以正常工作,但如果有三个结果,则一组数据是重复的(在本例中,它的id为1691):


如果我添加额外的WHERE子句来尝试阻止这种情况,它将返回零行,因此我认为我的连接语法在某个地方不正确。有什么想法吗?

基本条目是否始终为空?此外,您还可以为条目显示H、L、D。对于每一个产品,它们是否都是唯一的?当bundle\u code=10时,产品始终为空,当bundle\u code=20时,产品始终不为空。每个bundle_代码应该只有一个H,每个bundle_代码应该有一个或两个L。最后一点是给我带来麻烦的地方。它不-返回零结果。正如我提到的,我尝试过两种类似的方法,但结果总是一无所获。。删除where并将它们放入上述查询中各个表的联接中,这应该对您有用。您的意思是:
内部联接作为e2-ON(e1.bundle\u-id=e2.bundle\u-id和e1.bundle\u-code='10')
?这与GROUP BY相结合,似乎已经解决了这个问题。谢谢
SELECT e1.bundle_id AS b_id, e1.product AS prod, e1.id AS id, 
e2.bundle_id AS b2_id, e2.product AS prod2, e2.id AS id2,
e3.bundle_id AS b3_id, e3.product AS prod3, e3.id AS id3,
e4.bundle_id AS b4_id, e4.product AS prod4, e4.id AS id4,    
FROM `staging` AS e1
INNER JOIN `staging` AS e2 ON (e1.bundle_id = e2.bundle_id AND e1.id != e2.id)
LEFT JOIN `staging` AS e3 ON (e1.bundle_id = e3.bundle_id AND e2.id != e3.id AND e3.id != e1.id AND e3.product != 'H')
LEFT JOIN `staging` AS e4 ON (e1.bundle_id = e4.bundle_id AND e3.id != e4.id AND e4.id != e2.id AND e4.id != e1.id AND e4.product != 'H')
WHERE e1.bundle_code = '10'
AND e2.bundle_code = '20'
AND e2.product = 'H';
SELECT e1.bundle_id AS b_id, e1.product AS prod, e1.id AS id, 
e2.bundle_id AS b2_id, e2.product AS prod2, e2.id AS id2,
e3.bundle_id AS b3_id, e3.product AS prod3, e3.id AS id3,
e4.bundle_id AS b4_id, e4.product AS prod4, e4.id AS id4,    
FROM `staging` AS e1
INNER JOIN `staging` AS e2 ON (e1.bundle_id = e2.bundle_id AND e1.id != e2.id)
LEFT JOIN `staging` AS e3 ON (e1.bundle_id = e3.bundle_id AND e2.id != e3.id AND e3.id != e1.id AND e3.product != 'H')
LEFT JOIN `staging` AS e4 ON (e1.bundle_id = e4.bundle_id AND e3.id != e4.id AND e4.id != e2.id AND e4.id != e1.id AND e4.product != 'H')
WHERE e1.bundle_code = '10'
AND e2.bundle_code = '20'
AND e2.product = 'H';