大家帮我参谋一下这个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