【问题标题】:SELECT item types not provided by an entitySELECT 实体未提供的项目类型
【发布时间】:2010-11-07 03:55:20
【问题描述】:

所以我有一些数据。有实体。实体有任意数量的项目。项目可以是一组定义的类型之一。一个实体可以有多个给定类型的项目。我可以获得实体拥有的项目列表。我想要的是获取实体没有项目的类型列表。

这是我的架构:

entities
id name
1  Bob
2  Alice

item_types
id      name 
1       red
2       yellow
3       green
4       blue
5       orange

items
entity_id item_type_id name
1         1            apple
1         2            banana
1         3            lime
1         3            tree
2         3            money
2         5            traffic cone

我想查询 Bob 的 id (1) 并得到这个列表:

4   blue
5   orange

并查询 Alice 的 id (2) 并得到:

1   red
2   yellow
4   blue

这可能让我很生气。我会继续努力,但我敢打赌,你一定会偷看我的。感谢您的宝贵时间。

【问题讨论】:

    标签: sql select not-exists


    【解决方案1】:
    select id, name
    from item_types
    where id not in
        (select i.item_type_id
        from items i
        inner join entities e
            on e.id = t.entity_id
        where e.Name = 'Bob')
    

    或(有时更快,但优化器一直在变得更好):

    select disctinct t.id, t.name
    from item_types t
    left outer join items i
        on i.item_type_id = t.id
    left outer join entities e
        on e.id = i.entity_id
        and e.Name = 'Bob'
    where e.id is null
    

    【讨论】:

      【解决方案2】:

      给鲍勃

      SELECT
        t.id, t.name
      FROM
        items i
      INNER JOIN
        entities e ON e.id = i.entity_id
      INNER JOIN
        item_types t ON t.id = i.item_type_id
      WHERE
        e.id <> 1
      

      对于 Alice,只需将 e.id 1 交换为 e.id 2

      【讨论】:

        【解决方案3】:

        我想这就是你要找的:

        SELECT id, name
        FROM item_types
        WHERE id NOT IN
        (
            SELECT DISTINCT item_type_id
            FROM items
            WHERE entity_id = 1
        )
        

        “entity_id = 1”代表 Bob,根据需要进行更改。

        【讨论】:

          【解决方案4】:

          我会修改它以使其更好,但这是一个可行的解决方案

          set nocount on
          go
          drop table #entities
          drop table #itemtype
          drop table #items
          create table #Entities
          (
          EntityId int,
          EntityName  varchar (250)
          )
          create table #ItemType
          (
          ItemTypeId int,
          ItemTypeName    varchar (250)
          )
          
          create table #Items
          (
          EntityId int,
          ItemTypeId int,
          ItemName    varchar (250)
          )
          go
          insert into #entities values (1, 'Bob')
          insert into #entities values (2, 'Alice')
          go
          insert into #ItemType values (1, 'red')
          insert into #ItemType values (2, 'yellow')
          insert into #ItemType values (3, 'green')
          insert into #ItemType values (4, 'blue')
          insert into #ItemType values (5, 'orange')
          go
          insert into #Items values (1, 1, 'apple')
          insert into #Items values (1, 2, 'banana')
          insert into #Items values (1, 3, 'lime')
          insert into #Items values (1, 3, 'tree')
          insert into #Items values (2, 3, 'money')
          insert into #Items values (2, 5, 'traffic cone')
          go
          
          
          ;WITH ENTITY AS (
          SELECT #Entities.EntityId, EntityName, ItemTypeId, ItemName
          FROM #Entities, #Items
          WHERE #Entities.EntityId = #Items.EntityId
          AND #Entities.EntityName = 'Bob'
          ) 
          SELECT #ItemType.* FROM ENTITY
          RIGHT JOIN #ItemType ON ENTITY.ItemTypeId = #ItemType.ItemTypeId
          WHERE EntityId is NULL
          
          
          ;WITH ENTITY AS (
          SELECT #Entities.EntityId, EntityName, ItemTypeId, ItemName
          FROM #Entities, #Items
          WHERE #Entities.EntityId = #Items.EntityId
          AND #Entities.EntityName = 'Alice'
          ) 
          SELECT #ItemType.* FROM ENTITY
          RIGHT JOIN #ItemType ON ENTITY.ItemTypeId = #ItemType.ItemTypeId
          WHERE EntityId is NULL
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-11-03
            • 2012-12-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多