7

假设我有一个数据库,其中包含人员、杂货店和您可以在商店中购买的物品,如下所示:

Stores               People                Foods
-----------------    ------------------    ------------------
| id |   name   |    | id |   name    |    | id |   name    |
-----------------    ------------------    ------------------
| 1  |  Giant   |    | 1  | Jon Skeet |    | 1  | Tomatoes  |
| 2  |  Vons    |    | 2  | KLee1     |    | 2  | Apples    |
| 3  | Safeway  |    ------------------    | 3  | Potatoes  |
-----------------                          ------------------

我有一个额外的表来跟踪哪些商店卖什么:

Inventory
--------------------
| store_id| food_id|
--------------------
| 1       | 1      |
| 1       | 2      |
| 2       | 1      |
| 3       | 1      |
| 3       | 2      |
| 3       | 3      |
--------------------

我还有另一张桌子,上面有购物清单

Lists
---------------------
| person_id| food_id|
---------------------
| 1        | 1      |
| 1        | 2      |
| 1        | 3      |
| 2        | 1      |
| 2        | 3      |
---------------------

我的问题是,给定一个人或他们的 ID,找出他们可以去哪些商店的最佳方法是什么,以便他们将所有东西都列在他们的清单上。MySQL中这些类型的计算是否有模式?

我的尝试(非常丑陋和混乱)是这样的:

-- Given that _pid is the person_id we want to get the list of stores for.

SELECT stores.name, store_id, num, COUNT(*) AS counter
FROM lists
    INNER JOIN inventory 
        ON (lists.food_id=inventory.food_id)
    INNER JOIN (SELECT COUNT(*) AS num
            FROM lists WHERE person_id=_pid 
            GROUP BY person_id) AS T
    INNER JOIN stores ON (stores.id=store_id)
WHERE person_id=_pid 
GROUP BY store_id
HAVING counter >= num;

谢谢你的时间!

用数据编辑SQL 小提琴

4

2 回答 2

3

如果我要解决这个问题,我会将这四个表与它们的链接列(特别是外键)连接起来,然后在子句上创建一个子查询HAVING来计算每个人的列表中的项目数。试试这个,

SET @personID := 1;

SELECT  c.name
FROM    Inventory a
        INNER JOIN Foods b
            ON a.food_id = b.id
        INNER JOIN Stores c
            ON a.store_id = c.id
        INNER JOIN Lists d
            ON d.food_id = b.id
WHERE   d.person_id = @personID
GROUP BY c.name
HAVING   COUNT(DISTINCT d.food_id) =
     (
        SELECT COUNT(*)
        FROM Lists
        WHERE person_ID = @personID
     )

SQLFiddle 演示

于 2012-09-21T00:29:42.003 回答
1

@JohnWoo:为什么不同?

另一个...

SET @pid=2;

SELECT store_id, name
FROM inventory 
  JOIN lists ON inventory.food_id=lists.food_id
  JOIN stores ON store_id=stores.id
WHERE person_id=@pid
GROUP BY store_id
HAVING COUNT(*)=(
  SELECT COUNT(*)
  FROM lists 
  WHERE person_id=@pid
);
于 2012-09-21T00:49:23.333 回答