假设我在1NF中有一个带有以下表格的MySQL数据库:
sandwich_id | sandwich_name | sandwich_price
_______________________________________________
0 | BLT | 5.5
1 | Reuben | 7.0
3 | Grilled Cheese | 3.75
...
和一个存储所有成分值的单独表格:
sandwich_id | ingredient
__________________________
0 | bacon
0 | lettuce
0 | tomato
1 | corned beef
1 | swiss cheese
...
如何比较所有三明治的成分,以确定哪些最相似?
(另外,我缺少第二个表的技术术语吗?我想把它称为地图表,但我知道这不太对,因为地图表存储了两个表的外键并且这个更多的是第一个分支......)
答案 0 :(得分:1)
select sw1.sandwich_name, sw2.sandwich_name, COUNT(i1.ingredient) as [Ingredients in common]
from sandwiches sw1
join sandwiches sw2 on sw2.sandwich_id < sw1.sandwich_id
join ingredients i1 on i1.sandwich_id = sw1.sandwich_id
where exists (select 1
from sandwiches sw
join ingredients i on i.sandwich_id = sw.sandwich_id
where i.ingredient = i1.ingredient
and sw.sandwich_id = sw2.sandwich_id)
group by sw1.sandwich_name, sw2.sandwich_name
order by [Ingredients in common] desc, sw1.sandwich_name, sw2.sandwich_name