0

我有一张如下表:

创建表table1(id整数,名字文本,姓氏文本);

id 名字 姓氏

====================
1 本·泰勒
2 抢劫·泰勒
3 抢劫·史密斯
4 抢劫僵尸
5 彼得·史密斯
6 本·史密斯
7 彼得·泰勒

现在我有另一个表 create table table2(id integer,position integer);

身份位置

===========
1 5
1 9
2 6
3 7
6 2

我想选择带有 lastname 的行,其中 lastname 必须由 ben 和 rob 共享,firstnames 必须是 ben 和 rob。
现在我还希望 ben 的位置比 rob 的位置小一,因此结果将是:

id 名字 姓氏 位置

============================
1 本泰勒 5
2 抢泰勒 6

sql查询应该是什么?

4

3 回答 3

2

要获取姓氏:

select lastname
from names n join
     position p
     on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname
having count(distinct firstname) = 2 and
       1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)

然后,您可以通过以下方式获取原始列表:

select n.*, p.position
from names n join
     position p
     on n.id = p.id
where firstname in ('ben', 'rob') and
      lastname in (select lastname
                   from names n join
                        position p
                        on n.id = p.id
                   where firstname in ('ben', 'rob')
                   group by lastname
                   having count(distinct firstname) = 2 and
                          1+max(case when firstname = 'ben' then p.position end) = max(case when firstname = 'rob' then p.position end)
                  )

我认为以下查询回答了您的问题,但需要注意的是,这会将名称组合成一行:

select nben.*, p.position, nrob.*, prob.position
from names nben join
     positions p
     on nben.id = p.id and
        nben.firstname = 'ben' join
     names nrob
     on nrob.firstname = 'rob' and
        nrob.lastname = nben.lastname join
     positions prob
     on nrob.id = prob.id and
        p.position = prob.position - 1

此外,这是未经测试的。

于 2013-01-14T17:34:12.337 回答
0

这给了我需要从 Gordon Linoff 的解决方案中获得的结果:
select * from(select n.*, p.position
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob' ) 和
lastname in (select lastname
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname with
count(distinct firstname) = 2

              )
         )

as x,names,position
where
(x.id!=names.id and names.id=position.id and names.lastname=x.lastname
and (x.firstname='rob' and names.firstname='ben')和 x.position=position.position+1)

(x.id!=names.id and names.id=position.id and names.lastname=x.lastname
and (x.firstname='ben' and names.firstname ='rob') 和 x.position+1=position.position)


于 2013-01-15T04:25:58.797 回答
-1

Gordon Linoff 的解决方案:
select nben.*, p.position
from names nben join
position p
on nben.id = p.id and
nben.firstname = 'ben' join
names nrob
on nrob.firstname = 'rob' and
nrob.lastname = nben.lastname 在 nrob.id = prob.id 和p.position = prob.position 上加入
位置概率 - 1 给出与以下相同的结果:





select * from(select n.*, p.position
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob') and
lastname in (select lastname
from names n join
position p
on n.id = p.id
where firstname in ('ben', 'rob')
group by lastname with
count(distinct firstname) = 2

          )
     )

as x,names,position
where (x.id!=names.id and names.id=position.id
and names.lastname=x.lastname
and (x.firstname='ben' and names.firstname='rob')
和 x.position+1=position.position)

于 2013-01-15T04:49:47.187 回答