一个有点绕弯的sql查询

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:13:32
一个表叫table,里面两个字段 ,名字 ,城市
知道我的名字,
找出在这个table中,和我一个城市的人

怎么写这个查询

select 名字 from table where 城市 in (select 城市 from table where 名字=我的名字)

select * from table a
where exist (select * from table b where a.城市=b.城市 and a.名字='我的名字')

或者,用自联接:
select a.* from table a join table b on a.城市=b.城市 and b.名字='我的名字'

或者:
select * from table
where 城市 in (select 城市 from table where 名字='我的名字')

或者不用in,但是当“我的名字”在多个城市时,就只能查到第一个城市的人,第二个城市及以后的就查不到了。
select * from table
where 城市 = (select 城市 from table where 名字='我的名字')

select b.名字
from table a, table b
where a.城市 = b.城市
and a.名字 = 'xxx'
and b.名字 <> 'xxx'

select * from [table] where 城市 in
(select [城市] from [table] where [名字]='...')

select [名字]
from table
where [城市] = (select top 1 [城市] from table where [名字] = '我的名字')