0

I have a table in database and a column named timedate the time is stored like this in this column: 2013-05-25 12:15:25

I need to SELECT the row with the time within last 15 minutes only; so in the case above

SELECT if timedate is after 2013-05-25 12:00:25

I tried:

TO_DAYS(NOW()) - TO_DAYS('timedate') < 15

but it didn't work

SELECT 
    * 
  FROM 
    `column` 
  WHERE 
    `name` = 'name' 
    AND `family` = 'family'  
    AND (
      `test1` = 'test1 value' 
      || `test2` = 'test2 value' 
      || `test3` = 'test3 value' 
    ) 
    AND TO_DAYS(NOW()) - TO_DAYS('timedate') < 15 
  LIMIT 
    1
hakre
  • 184,866
  • 48
  • 414
  • 792
Vladimir
  • 1,582
  • 16
  • 39

5 Answers5

1
SELECT * FROM `column` 
WHERE `name` = {$name} 
AND `family` = {$family}  
AND (`test1` = {$test1} || `test2` = {$test2} || `test3` = {$test3}) 
AND `timedate` > NOW() - INTERVAL 15 MINUTE

Important: Note that I replaced your ' around timedate with backticks. With ' MySQL thinks it's a string, not a column name.

fancyPants
  • 49,071
  • 32
  • 84
  • 94
1

If type of field test1, test2 and test3 is number, then your query should be :

SELECT 
  * 
FROM
  `column` 
WHERE `name` = '{$name}' 
  AND `family` = '{$family}'
  AND (
    `test1` = {$test1} || `test2` = {$test2} || `test3` = {$test3}
  ) 
  AND ADDTIME(`timedate`, '00:15:00.000000') > NOW()

And if type of field test1, test2 and test3 is varchar, your query should be :

SELECT 
  * 
FROM
  `column` 
WHERE `name` = '{$name}' 
  AND `family` = '{$family}'
  AND (
    `test1` = '{$test1}' || `test2` = '{$test2}' || `test3` = '{$test3}'
  ) 
  AND ADDTIME(`timedate`, '00:15:00.000000') > NOW()

Hopefully this help.

0

See this page:

You can use DATE_SUB(CURDATE(), INTERVAL 15:00 MINUTE_SECOND) to get the time 15 minutes ago, and then compare the dates with the > operator.

pascalhein
  • 5,530
  • 4
  • 30
  • 41
0

Try TIMESTAMPDIFF (MINUTE, NOW(), timestamp)

See the MySQL manual

nurdglaw
  • 2,031
  • 17
  • 33
0

try something like this

SELECT TIMEDIFF(NOW(),`timedate`) < 15 FROM ......
srakl
  • 2,475
  • 2
  • 19
  • 32
  • i don't think this will work, as the output is like 46:58:57.999999 it will work for hours not minutes – Vladimir Jul 21 '13 at 12:44