0

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

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

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

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

我继续前进7 and 8。正确答案应仅包含以下 ID1 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"
4

2 回答 2

2

尝试类似的东西

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

将您的and选择器换成or.

于 2013-08-28T10:04:50.353 回答
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) );
于 2013-08-28T10:04:51.573 回答