1
Table A: Person: id, name
Table B: Toys: id, person_id, toy_name

I have a search screen that includes a dropdown of fixed toy names. A search is found if a subset of the total set of toys for a person is matched.

Example, a person name=bob has toys: doll, car, house, hat

A search is done for person name=bob and toys=doll, hat.

I want to return bob and ALL of his toys, not just what toys were searched for(doll, hat). Bob is found because a subset of his toys are a match.

I don't know what the most efficient/least db calls way to accomplish this. I can do a search for bob and get all of his toys, then parse through the result set to see if the searched for toys find a match, but that seems wrong, that the db call could return rows for which no match is found (and that seems wrong?).

4

2 回答 2

2

好的,

select
  p.id,
  p.name, 
  t.id as toyid, 
  t.toy_name
from 
     person p
  join
     toys t
        on p.id = t.person_id
where
     p.id in (
        select person_id from toys where toy_name = 'doll'
        intersect
        select person_id from toys where toy_name = 'hat');

在这里提琴


如果你进一步规范化你的模式,

create table Person
(
    Id int,
    Name varchar(100)
);

create table Toy
(
    Id int, 
    Name varchar(100)
);

create table PersonToy
(
    Id int,
    PersonId int,
    ToyId int
);

它应该使问题的复杂性更加清晰。它还将节省一些空间。表格声明,

select
            p.Name PersonName,
            t.Name ToyName
    from
            Person p
        join
            PersonToy pt
                on pt.PersonId = p.Id
        join
            Toy t
                on t.Id = pt.ToyId
    where
        p.Id in
        (
            select PersonId from PersonToy where ToyId = 1
            intersect
            select PersonId from PersonToy where ToyId = 4
        );

将有效地工作。

更新小提琴

于 2013-04-30T15:36:20.137 回答
0

这是使用子查询并检查HAVING子句中是否存在 Hat 和 Doll 的一种方法:

select p.id, p.name, 
  t.id as toyid, t.name as toyname
from person p
  inner join toys t on p.id = t.person_id
  inner join (
    select person_id
    from toys 
    group by person_id
    having sum(name = 'hat') > 0 and
      sum(name = 'doll') > 0
    ) t2 on p.id = t2.person_id

SQL 小提琴演示

于 2013-04-30T15:27:37.403 回答