![](/img/trans.png)
[英]sql query for filtering data with where multiple criteria on same column of table
[英]SQL query to show data that exactly match criteria in another table's column
到此时,我正在实现一个在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满足了在“ 水果和动物”中具有“ 比赛ID”中指定的关系且具有以下格式的所有条件的条件:
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
所有解决方案均受到高度赞赏,因此感谢您的帮助。
这是执行此操作的一种方法,但这将是对大型集合进行轻度调光的查询。
SQL Fiddle演示在这里: 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。
最后,我们可以将其重新连接到用户实际拥有的商品,以便显示结果。
(同样,这将是可怕的调光器,尽管索引会有所帮助。)
尝试这个:
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'
使用它,它的工作原理:
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
对于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;
您在table3中有一个缺陷:1个数据(fruit)的2个ID(假设itemtype将是外键),否则查询将是:
select *
from table1
join table2 using (item)
join table3 using (itemtype)
当然假设
itemtype是表2主键
itemtype是表3表2的外键
项目是表1表2的外键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.