2

到这个时候,我正在实现一个在 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

非常感谢所有解决方案,因此感谢您的帮助。

4

5 回答 5

1

这是执行此操作的一种方法,但这将是对大型设备的调光查询。

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

别名为 as 的内联视图n表示用户需要拥有的东西,以及满足每个 match_id 所需的所有 item_type。

别名为 as 的内联视图h表示用户实际拥有的内容,即用户对每个 match_id 拥有的所有 item_type。

我们可以得到每个集合中不同 item_type 的计数,并比较计数。如果计数相等,则我们知道用户具有该 match_id 所需的所有 item_type。

最后,我们可以将它加入到用户实际拥有的项目中,这样我们就可以显示结果。

(同样,这将是可怕的调光器,尽管索引会有所帮助。)

于 2013-07-25T06:50:18.160 回答
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'
于 2013-07-25T05:04:31.820 回答
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 
于 2013-07-25T06:22:53.890 回答
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;
于 2013-07-25T07:11:58.793 回答
0

您在 table3 中有一个缺陷:1 个数据(fruit)的 2 个 ID(假设 itemtype 将是外键),否则查询将是:

select *
from table1 
   join table2 using (item)
   join table3 using (itemtype)

假设,当然

  1. itemtype 是表 2 主键

  2. itemtype 是表 3 到表 2 的外键

  3. item 是表 1 到表 2 的外键

于 2013-07-25T10:01:15.567 回答