ASP里,两张表联合查询问题。

表1:baodanIDbaodanhaochezhu14343张三255545李四表2:huifangIDbaodanIDhuifangshijian112010-09-... 表1:baodan
ID baodanhao chezhu
1 4343 张三
2 55545 李四

表2:huifang
ID baodanID huifangshijian
1 1 2010-09-12
2 1 2010-10-12
3 1 2010-11-12
4 2 2010-09-12
5 2 2010-12-12
baodan.id=huifang.baodanID,我想查询出表2:huifang中最近的huifangshijian超过3个月的保单,列表出来,并且可以用chezhu做查询。怎么搞?
谢谢!
想得到的结果为:
ID baodanhao chezhu huifangshijian
1 4343 张三 2010-11-12
2 55545 李四 2010-12-12
展开
 我来答
cyks1
2011-01-11 · TA获得超过193个赞
知道小有建树答主
回答量:246
采纳率:100%
帮助的人:252万
展开全部
SELECT baodan.ID, baodan.baodanhao, baodan.chezhu, huifang.huifangshijian
FROM baodan INNER JOIN huifang ON baodan.ID = huifang.baodanID

至于后面“huifang中最近的huifangshijian超过3个月的保单”,我没看明白是什么意思
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
谖草芄兰
2011-01-11 · TA获得超过105个赞
知道答主
回答量:48
采纳率:0%
帮助的人:29.7万
展开全部
SELECT baodan.ID, baodan.baodanhao, baodan.chezhu, huifang.huifangshijian
FROM baodan INNER JOIN huifang ON baodan.ID = huifang.baodanID where DATEDIFF(mm,getdate(),huifang. huifangshijian) >= 3
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
shenchaoliang
2011-01-11 · TA获得超过1163个赞
知道大有可为答主
回答量:810
采纳率:0%
帮助的人:1510万
展开全部
sql server?
最近的
SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian)
FROM baodan as a, huifang as b
WHERE a.id = b.baodanID
group by a.id,a.baodanhao,a.chezhu
超90天最近的
SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian)
FROM baodan as a, huifang as b
WHERE a.id = b.baodanID
and datediff(d,b.huifangshijian,getdate()) > 90
group by a.id,a.baodanhao,a.chezhu
超3个月最近的
SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian)
FROM baodan as a, huifang as b
WHERE a.id = b.baodanID
and datediff(m,b.huifangshijian,getdate()) > 3
group by a.id,a.baodanhao,a.chezhu
要加chezhu查询的话
SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian)
FROM baodan as a, huifang as b
WHERE a.id = b.baodanID
and datediff(m,b.huifangshijian,getdate()) > 3
and a.chezhu = '张三'
group by a.id,a.baodanhao,a.chezhu

asp中

<%
If chezhu = "" then
sql = "SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian) FROM baodan as a, huifang as b WHERE a.id = b.baodanID and datediff(m,b.huifangshijian,getdate()) > 3 group by a.id,a.baodanhao,a.chezhu"
Else
sql = "SELECT a.id,a.baodanhao,a.chezhu,max(b.huifangshijian) FROM baodan as a, huifang as b WHERE a.id = b.baodanID and datediff(m,b.huifangshijian,getdate()) > 3 and a.chezhu = '" & chezhu & "' group by a.id,a.baodanhao,a.chezhu"
End if

'执行语句
%>
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式