查询以获取另一个表的字符串列中不存在的一个表的值,也小于特定的日期时间值

时间:2014-05-25 15:32:02

标签: mysql sql

我有两张桌子:

mysql> select * from quizquestionbank;

| ID | QuestionFilePath | CorrectAnswer | EndDate            |
--------------------------------------------------------------
|  1 | p.wav            |             1 |2014-05-12 12:00:00 |
|  2 | q.wav            |             2 |2014-05-12 12:00:00 |
|  3 | a.wav            |             3 |2014-05-12 12:00:00 |
|  4 | b.wav            |             1 |2014-05-12 12:00:00 |
|  5 | m.wav            |             3 |2014-05-12 12:00:00 |

第二张表是:

mysql> select * from quizuserdetails;

| ID | MSISDN | QuestionIdDetails | AnswerRecord |
--------------------------------------------------
|  1 | 235346 | 1,3,4,5           | S,F,S,F      |
|  2 | 564574 | 4,5,67,88         | F,S,F,s      |
|  3 | 500574 | 5,55,66,44,2      | F,F,F,F      |

我想从表1中获取ID:
 1.在第二张表的QuestionIdDetails栏中没有和  2.少于当前的日期和时间。

以下查询为我提供了第一点所需的记录:

Select qb.ID,qb.EndDate 
from quizquestionbank qb 
     left join quizuserdetails qd 
     on find_in_set(qb.id, QuestionIdDetails) > 0  
     and msisdn = '235346' 
where qd.id is null

但是对于第二个要求,查询后会出现错误:

Select * 
from predictionfootball 
where '2014-05-10 00:00:00' < 
    (Select qb.ID,qb.EndDate 
    from quizquestionbank qb 
         left join quizuserdetails qd 
         on find_in_set(qb.id, QuestionIdDetails) > 0  
         and msisdn = '235346' 
    where qd.id is null)

请告诉我如何做到这一点。

1 个答案:

答案 0 :(得分:0)

试试:

 Select qb.ID,qb.EndDate 
 from quizquestionbank qb 
 left join quizuserdetails qd 
 on find_in_set(qb.id, QuestionIdDetails) > 0  
 and msisdn = '235346' 
 where qd.id is null
 AND qb.EndDate < '2014-05-10 00:00:00'    ------> added this line