求SQL语句

zhui22222 2012-08-24 10:44:18
表TA字段:编码,内容。用于保存每个编码对应的具体内容。
表TB字段:序号B(自增),编码,字段2,字段3,...
表TC字段:序号C(自增),编码,...
要求查询结果:序号B,内容(将编码替换为内容),标记(是否能在C中找到),字段2,字段3,...
...全文
86 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
青天流雲 2012-08-24
  • 打赏
  • 举报
回复
同问,能不能按某一条件查出某一个值的个数呢??
quchen520 2012-08-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

SQL code

select TB.序号B,TA.内容,case when (select count(*) from TC where TB.编码=TC.编码)>0 then 1 else 0 end 标记,字段2,字段3... from TB
left join TA
on TB.编码=TA.编码
[/Quote]+1
2楼正解
ORAClE SE 2012-08-24
  • 打赏
  • 举报
回复
表与表连接便可做到。
dw1258838824 2012-08-24
  • 打赏
  • 举报
回复
额 我肚子好饿啊
Aaron_Chan 2012-08-24
  • 打赏
  • 举报
回复


create table #t1(id int,content varchar(10))
create table #t2(bid int,id int)
create table #t3(cid int,id int)
insert into #t1 select 1,'内容1' union select 2,'内容2'
insert into #t2 select 1,1 union select 2,2
insert into #t3 select 1,1

select #t1.id,content,flag=case when cid is null then '否' else '是' end from #t1
inner join #t2 on #t1.id=#t2.id left join #t3 on #t1.id=#t3.id

id content flag
----------- ---------- ----
1 内容1 是
2 内容2 否
  • 打赏
  • 举报
回复

select TB.序号B,TA.内容,case when (select count(*) from TC where TB.编码=TC.编码)>0 then 1 else 0 end 标记,字段2,字段3... from TB
left join TA
on TB.编码=TA.编码
zhui22222 2012-08-24
  • 打赏
  • 举报
回复
SQL Server的

34,590

社区成员

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

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