此时我正在实现一个在3个表之间执行匹配的系统,我现在真的需要你的帮助,假设我有以下三个表:
表1:名称和项目之间的关系
User Item
=====================
John Doe Apple
John Doe Orange
John Doe Cat
John Doe Dog
John Doe Fish
Anna Sue Apple
Anna Sue Orange
Robinson Banana
Robinson Vessel
Robinson Car
表2:对商品进行分类
Item Type Item
==================
Fruit Apple
Fruit Orange
Fruit Banana
Animal Cat
Animal Dog
Vehicle Vessel
Vehicle Car
Vehicle Truck
表3:项目匹配
Match ID Item Type
======================
M001 Fruit
M001 Animal
M002 Fruit
M002 Vehicle
我只想问我如何只显示所有标准与指定匹配ID完全匹配的用户
对于这种情况,用户 John Doe 满足在 Fruit 和 Animal 中具有项目的所有标准匹配ID < / strong>具有以下格式:
User Match ID Item Type Item
================================================
John Doe M001 Fruit Apple
John Doe M001 Fruit Orange
John Doe M001 Animal Cat
John Doe M001 Animal Dog
Robinson M002 Fruit Banana
Robinson M002 Vehicle Vessel
Robinson M002 Vehicle Car
非常感谢所有解决方案,感谢您的帮助。
答案 0 :(得分:1)
这是一种方法,但这将是对大型集合的轻微调光查询。
SQL Fiddle demo here: http://sqlfiddle.com/#!2/63cd2/1
SELECT ui.user_name
, tm.match_id
, tm.item_type
, ui.item
FROM (SELECT uu.user_name
, tm.match_id
, COUNT(DISTINCT tm.item_type) AS cnt_item_type
FROM (SELECT u.user_name FROM user_item u GROUP BY u.user_name) uu
CROSS
JOIN type_match tm
GROUP BY uu.user_name, tm.match_id
) n
JOIN (SELECT hui.user_name
, htm.match_id
, COUNT(DISTINCT htm.item_type) AS cnt_item_type
FROM user_item hui
JOIN item_type hit ON hit.item = hui.item
JOIN type_match htm ON htm.item_type = hit.item_type
GROUP BY hui.user_name, htm.match_id
) h
ON h.cnt_item_type = n.cnt_item_type
AND h.match_id = n.match_id
AND h.user_name = n.user_name
JOIN user_item ui
ON ui.user_name = h.user_name
JOIN item_type it
ON it.item = ui.item
JOIN type_match tm
ON tm.item_type = it.item_type
AND tm.match_id = h.match_id
ORDER
BY ui.user_name
, tm.match_id
, tm.item_type
, ui.item
内嵌视图别名为 n
表示用户需要拥有的内容,以满足每个match_id所需的所有item_type。
内嵌视图别名为 h
表示用户实际拥有的内容,即用户对每个match_id的所有item_type。
我们可以计算每个集合中不同的item_type,并比较计数。如果计数相等,那么我们知道用户具有该match_id所需的所有item_type。
最后,我们可以将其加入到用户实际拥有的项目中,这样我们就可以显示结果。
(再次,这将是可怕的调光器,虽然索引会帮助一些。)
答案 1 :(得分:0)
试试这个:
SELECT [User], [Match ID], [Item Type],[Item]
From table1
Inner join table2 on table1.item = table2.item
Inner join table3 on table2.[item type]= table3.[item type]
Where [User] = 'SOME USER NAME' AND table2.[item type] = 'SOME ITEM TYPE' AND table1.Item = 'SOME ITEM'
答案 2 :(得分:0)
使用它,它的工作原理:
select t1.[User],t3.matchid,t3.item_type,t1.item from table3 t3 left join table2 t2 on t3.item_type=t2.Item_type left join table1 t1 on t2.Item=t1.Item where t1.[user]='JohnDoe' and t3.MatchId='m001' group by t1.[user],t1.item,t3.MatchId,t3.Item_Type
答案 3 :(得分:0)
对于MySQL
select t1.User,t3.MatchID,t3.ItemType as ItemType,t2.Item as Item
from Table1 t1
inner join Table2 t2 on t1.Item = t2.Item
inner join Table3 t3 on t3.ItemType = t2.ItemType
inner join
(select user,MatchID
from
(SELECT GROUP_CONCAT(ItemType ORDER BY ItemType) AS typesTomatch , MatchID
FROM Table3 GROUP BY MatchID) abc
inner join
(Select a.User, group_concat(distinct b.ItemType ORDER BY b.ItemType)
as typesofpeople
from Table1 As a
inner join Table2 As b on a.Item = b.Item
group by a.User order by b.ItemType) def
on abc.typesTomatch = def.TYPESOFPEOPLE) xyz
on xyz.User = t1.User and xyz.MatchID = t3.MatchID;
答案 4 :(得分:0)
你有1个数据(水果)的table3:2 ID(假设itemtype将是外键),否则查询将是:
select *
from table1
join table2 using (item)
join table3 using (itemtype)
假设,当然
itemtype是表2主键
itemtype是表2的表3外键
item是表2的表1外键