数据表a
字段 id word
1 loli
2 view
3 数据表b
字段 name gid
huang 3
zhang 2
luo 1
li 2其中表a的id字段和表b的gid字段值是一一对应的,请问怎么找出word为loli的luo姓?
我写的是:
select a.name from a left join b on a.word='loli' and b.gid=a.id
但是结果是表b的四个人都被找出来了,而我只要luo一个人
字段 id word
1 loli
2 view
3 数据表b
字段 name gid
huang 3
zhang 2
luo 1
li 2其中表a的id字段和表b的gid字段值是一一对应的,请问怎么找出word为loli的luo姓?
我写的是:
select a.name from a left join b on a.word='loli' and b.gid=a.id
但是结果是表b的四个人都被找出来了,而我只要luo一个人
select b.name from b left join a on a.word='loli' and b.gid=a.id
inner join a on a.ID=b.GID
where a.word='loli'
select b.name from b
left join a on a.ID=b.GID
where a.word='loli'
where a.id=b.gid and a.word='loli'
这个可以搜索到了,你试试.
if object_id('[a]') is not null drop table [a]
go
create table [a]([id] int,[word] varchar(4))
insert [a]
select 1,'loli' union all
select 2,'view' union all
select 3,''
if object_id('[b]') is not null drop table [b]
go
create table [b]([name] varchar(5),[gid] int)
insert [b]
select 'huang',3 union all
select 'zhang',2 union all
select 'luo',1 union all
select 'li',2
---查询---
select b.name
from a,b
where a.id=b.gid
and a.word='loli'---结果---
name
-----
luo(所影响的行数为 1 行)