首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

大家帮小弟我参谋一下这个SQL语句

2011-12-25 
大家帮我参谋一下这个SQL语句表AidA1A2(默认值)1******2******表BA_IDA3A4Date1****2007-6-201****2007-6-

大家帮我参谋一下这个SQL语句
表A
      id           A1           A2   (默认值)  
      1           ***           ***
      2           ***           ***

表B
    A_ID         A3         A4       Date
    1               **         **       2007-6-20
    1               **         **       2007-6-21

select   A.A1,A.A2,B.A3,B.A4   from   A   left   join   B   on   A.id=B.A_ID   where   id=1   and   datediff(d,Date,getdate())=0

如果今天有值就返回A1,A3,A4,如果今天没有值就返回A2也就是默认值。我上面的写法肯定不行。现在没想出来有什么办法,大家帮我参谋一下。

[解决办法]
select Case WHEN B.A_ID IS NULL THEN A2 Else A1 END AS A1,B.A3,B.A4 from A left join B on A.id=B.A_ID where id=1 and datediff(d,Date,getdate())=0
[解决办法]
up

select
Case when B.A_ID is null then A2 else A1 end as A1,
B.A3,
B.A4
from A
left join B
on A.id=B.A_ID
where id=1 and datediff(d,Date,getdate())=0


[解决办法]
if exists(select * from datediff(d,Date,getdate())=0)
select a1, a3, a4 .......
else
select a2

热点排行