各种报错 2015-06-26 07:00 采纳率: 0%
浏览 1416
已结题

sql查询结果重复问题 是否表结构设计问题

信息表a
sid scope_type begin_date end_date
1 3
2 2
3 1

scope_type(1:按全部;2:按渠道;3:按机构)

范围表b
tid sid channel org_id
1 1 1 null
2 1 3 null
3 1 101

1.两表通过sid关联,没有设外键,
2.新增记录时,先写表a,再写表b
3.当scope_type=1时,则只需a表新增1条记录.
当scope_type不为1时,在a表新增1条记录,b表根据选择的机构和渠道,新增记录.
如选择了机构101,102,渠道1
则b表新增3条记录
4.查询时,根据时间范围,查出的结果有重复的,求解

 select 
b.*
from a, b
where a.sid = b.sid(+)
and a.sid = 1
and begin_date > ?
and end_date <= ?

ps:我考虑到两个方面:
1.表结构设计问题
2.sql方面

  • 写回答

6条回答

  • danielinbiti 2015-06-26 07:12
    关注
     select 
    distinct b.*  --distinct一下吧,
    from a, b
    where a.sid = b.sid(+)
    and a.sid = 1
    and begin_date > ?
    and end_date <= ?
    
    评论

报告相同问题?