选择添加少于一个月前和一个多月前的分数为X的所有记录

时间:2013-08-28 10:00:40

标签: mysql sql

从下表中,我正在尝试选择以下记录:

  • 已在上个月内添加(无论得分如何)
  • 一个多月前添加的记录,得分为> = 10

我的努力在下面,但我在某个地方出错了

select id from candidates 
where dateEnrolled >= date_sub(now(), interval 1 month) 
and dateEnrolled <= now() and score >=10;

我继续7 and 8。正确答案应仅包括以下ID 1 2 5 6 7 8 9 10

你能帮忙吗?

"id"    "dateEnrolled"  "score"
"1"     "2013-01-01"    "12"
"2"     "2013-02-01"    "15"
"3"     "2013-03-01"    "9"
"4"     "2013-04-01"    "8"
"5"     "2013-05-01"    "20"
"6"     "2013-08-01"    "0"
"7"     "2013-08-12"    "10"
"8"     "2013-08-13"    "12"
"9"     "2013-08-15"    "1"
"10"    "2013-08-17"    "5"

2 个答案:

答案 0 :(得分:2)

尝试类似

的内容
select id from candidates where dateEnrolled >= date_sub(now(), interval 1 month)
      or (dateEnrolled <= now() and score >=10);

and选择器换成or

答案 1 :(得分:1)

select id from candidates 
where dateEnrolled >= date_sub(now(), interval 1 month) 
and dateEnrolled <= now() 
or (score >=10 and dateEnrolled < date_sub(now(), interval 1 month) );