mysql,查询间隔错误

时间:2015-03-23 06:10:26

标签: mysql mysqli

select *, 
   (select max(h.date) 
    from hour_cards as h  
    where h.subordinate_id = hour_cards.subordinate_id
      and h.date > hour_cards.date and 
   h.date - hour_cards.date <= INTERVAL 1 minute) as wrong_entry
   from hour_cards

我试图进行查询,如果他/她在委托或退出工作时读了两次或更多的卡,我会给每个工人的所有条目,但上面的查询给我一次错误, #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') as wrong_entry from hour_cards

2 个答案:

答案 0 :(得分:0)

您只能在date calculation function中使用间隔。

您的查询必须如下:

select *, 
   (select max(h.date) 
    from hour_cards as h  
    where h.subordinate_id = hour_cards.subordinate_id
      and h.date > hour_cards.date and 
   DATE_ADD(h.date, INTERVAL 1 minute) <= hour_cards.date  as wrong_entry
   from hour_cards

答案 1 :(得分:0)

您可以使用TIMESTAMPDIFF使其正确:

 TIMESTAMPDIFF(MINUTE,h.date,hour_cards.date) <= 1