2013-08-28 31 views
0

的从下面的表格,我想选择的记录是:选择所有记录中,其中加入比一个月前不到一个月前多,得分X

  • 内部已经增加上个月
  • 记录但一个多月前加入,并有分数的(不管分)> = 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

试着这么做

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

交换你and选择一个or

+0

虽然我仍然无法理解它应该是'or'而不是'and',但我会弄清楚自己:-) – Norman

+0

记录被添加超过一个月前,并有> = 10的分数。所以不应该是“或(score> = 10和dateEnrolled = 10)“ –

+0

这两个条件都与dateEnrolled字段一起工作,尽管其中只有一个需要考虑分数。为了使这个条件起作用,你需要使用'Or'而不是和'And'来分割两个条件。在你使用'And'运算符的那一刻,所有的条件都必须是真实的。 – cbillowes

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)); 
+0

你sql中的额外'和dateEnrolled <= now()'是什么? – Norman

+0

筛选未来成员选择上月添加的成员。如果你没有将来的记录,它可以被删除。 –

相关问题