1

这是有问题的脚本:

select distinct person, min(pdate) as min_date from db
where ptype like 'A1%'
or (ptype like 'B1%'
and (pdate between '2000-01-01' and '2001-01-01'))
group by person

问题

检索的 min_date 还包括我指定的日期之外的那些。但是,当我取出其中一种 ptype 时,我有:

select distinct person, min(pdate) as min_date from db
where ptype like 'A1%'
and (pdate between '2000-01-01' and '2001-01-01')
group by person

然后这个问题就消失了。为什么引入第二个 ptype 会重新调整我指定的更新之外的实例?

谢谢。

4

2 回答 2

5

这是因为您现有的查询是以下形式:

IF A or (B and D)

- 所以如果 A 为真,则 D 被忽略。您实际上需要以下形式的查询:

IF (A or B) and D

像这样:

select distinct person, min(pdate) as min_date from db
where (ptype like 'A1%' or ptype like 'B1%') and 
      pdate between '2000-01-01' and '2001-01-01'
group by person
于 2013-02-14T16:19:22.990 回答
3
select distinct person, min(pdate) as min_date from db
where (      ptype like 'A1%'
         or (ptype like 'B1%'))
and (pdate between '2000-01-01' and '2001-01-01')
group by person

一)是假的?

于 2013-02-14T16:14:16.403 回答