请教一个sql语句

heidong2002 2010-07-29 04:03:38
表a:
code name
001 aaa
002 bbb
003 ccc
004 ddd
表b:
code info
002 0
想生成数据集:
code name info
001 aaa 1
002 bbb 0
003 ccc 1
004 ddd 1
如何写sql语句?
...全文
92 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xyj052 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shutao917 的回复:]
SQL code
select a.code,a.name,isnull(b.info,1)
from 表a a left join 表b b
on a.code=b.code
[/Quote]

UP......
luodx1021 2010-07-29
  • 打赏
  • 举报
回复
SELECT a.code, a.name, CASE WHEN b.info IS NULL THEN 1 ELSE b.info END AS info
FROM a LEFT JOIN b ON a.code = b.code
水中影子 2010-07-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 shutao917 的回复:]
SQL code
select a.code,a.name,isnull(b.info,1)
from 表a a left join 表b b
on a.code=b.code
[/Quote]

+1
shixixi1987 2010-07-29
  • 打赏
  • 举报
回复
if not object_id('a') is null
drop table a
Go
Create table a([code] varchar(10),[name] varchar(10))
Insert a
select '001','aaa' union all
select '002','bbb' union all
select '003','ccc' union all
select '004','ddd'
Go
if not object_id('b') is null
drop table b
Go
Create table b([code] varchar(10),info int)
Insert b
select '002',0
Go
select * from a
go
select * from b
go
select a.code,a.name,isnull(b.info,1) info from a left join b on a.code=b.code
go
zhaojianmi1 2010-07-29
  • 打赏
  • 举报
回复
select a.code, a.name, case b.info when 0 then 0 else 1 as info
from a left join b on a.code = b.code
Angel1201 2010-07-29
  • 打赏
  • 举报
回复
if not object_id('a') is null
drop table a
Go
Create table a([code] nvarchar(3),[name] nvarchar(3))
Insert a
select N'001',N'aaa' union all
select N'002',N'bbb' union all
select N'003',N'ccc' union all
select N'004',N'ddd'
Go
if not object_id('b') is null
drop table b
Go
Create table b([code] nvarchar(3),[info] int)
Insert b
select N'002',0
Go
select a.*,
isnull(b.info,1)info
from a left join b
on a.code=b.code
/*
code name info
---- ---- -----------
001 aaa 1
002 bbb 0
003 ccc 1
004 ddd 1


*/
xuam 2010-07-29
  • 打赏
  • 举报
回复
select a.code, a.name,isnull(b.info,1) from a left join b on a.code=b.code
情殇无限 2010-07-29
  • 打赏
  • 举报
回复
select a.code,a.name,isnull(b.info,1)
from 表a a left join 表b b
on a.code=b.code
Angel1201 2010-07-29
  • 打赏
  • 举报
回复
select a.*,
isnull(b.info,1)info
from a left join b
on a.code=b.code and a.name=b.name
天-笑 2010-07-29
  • 打赏
  • 举报
回复


select t1.code,t1.name,case when t2.info is null then 1 else t2.info end as info from a t1 left join b t2 on t1.code = t2.code

情殇无限 2010-07-29
  • 打赏
  • 举报
回复

select a.code,a.name,b.info
from 表a a,表b b
where a.code=b.code

34,593

社区成员

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

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