select * from a
left join b on a.uid=b.uid where b.viewflag=1 and b.endate>=getdate()
order by a.id desc
这个比inner join 快点
inner join 要相互匹配
left join 把左边的表 移到主表
数据不会丢失
select * from a where exitsts(select * from b where b.uid=a.uid and b.viewFlag=1 and b.EndDate>=getdate())>0) order by a.id desc
或者
select * from a inner join b on a.uid=b.uid
where b.viewflag=1 and b.endate>=getdate()
order by a.id desc
你的意思应该是处理某个逻辑时会运用到两张表的数据吧,实现方法有很多啊!关于java WEB数据库编程建议学一些基础框架!(如hibernate)
至于上面你提出的问题,你可以设计一个中间javabean,作为数据储存专用!
select * from a where a.uid in ( select b.uid from b where b.viewFlag=1 and b.EndDate>=getdate() group by b.uid ) order by a.id desc 这个执行效率不是很好
select * from a where exitsts
(select * from b where b.uid=a.uid and b.viewFlag=1 and b.EndDate>=getdate())
order by id desc