请教a表和b表关联,b中有多条数据对应a中的一条数据,如何找出符合条件的?具体在内容中

strife013 2009-07-26 07:21:29
请教a表和b表关联,b中有多条数据对应a中的一条数据,比如a.pid = b.pid and b.type=shop,这样的关联但是在b中有多条数据或没有数据关联a表的一条数据,如何找出a表中的全部数据并且b表中如果有多条关联的只返回对应第一条的,如果b没有数据则b表字段为空关联到a表,就是说要返回a的全部数据,而且数量和a表数据数量一致,这样的结果如何返回?
...全文
34 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
华夏小卒 2009-07-26
  • 打赏
  • 举报
回复

declare @a table(pid int,type varchar(10))
declare @b table(pid int,type varchar(10))

insert @a
select 1,'南京' union all
select 2,'无锡' union all
select 3,'镇江'

insert @b
select 1,'shop' union all
select 1,'www ' union all
select 3,'shop'


select a.pid,b.type from @a a
left join @b b
on a.pid=b.pid and b.type='shop'

/*
pid type

1 shop
2 null
3 shop
*/

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧