谁能告诉我如何在 MySQL 中创建对。如果您看到以下查询
select A.cakeid , A.ingredid from Contain as A inner join (select
cakeid, count(cakeid)
from
Contain
group by cakeid
having count(cakeid) >= 3 ) as B on A.cakeid = B.cakeid
OUTPUT:
Cakeid Ingredid
'1', '1'
'1', '3'
'1', '4'
'3', '1'
'3', '4'
'3', '5'
'5', '1'
'5', '2'
'5', '4'
'6', '1'
'6', '2'
'6', '3'
'6', '5'
'7', '1'
'7', '2'
'7', '3'
'7', '4'
'8', '1'
'8', '2'
'8', '3'
有人能告诉我如何创建至少使用三种常见成分的 Cakeid 吗?例如,6和7是一对,具有三个共同成分。
最佳答案
自加入
select c1.cakeid cake1, c2.cakeid cake2, count(*) matching_Ingredid
from Contain c1
join Contain c2
on c1.cakeid < c2.cakeid and c1.Ingredid = c2.Ingredid
group by c1.cakeid, c2.cakeid
having count(*) >= 3
关于mysql - 如何在sql查询中创建对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33097799/