tags:

views:

52

answers:

2

select query to select all records that were inserted in 15 minutes.for ex if now time is 10:00 then it shoulld fetch all record inserted from 9:45 to 10:00

+2  A: 

Assuming you have a DATETIME column named created in your table:

SELECT id FROM tablename 
WHERE ((created > DATE_SUB(NOW(), INTERVAL 15 MINUTE))
  AND (created < NOW()))
Pekka
What's the second condition for? To prevent against time traveling?
Artefacto
@Artefacto you never know! :)
Pekka
`if bool(foo) == True and True is True: print 'foo is True'`
shylent
@shylent not true. It is perfectly possible to insert future dates into the `created` column. There is no native mySQL way to stop anybody from doing so. Better safe than sorry.
Pekka
@Pekka: don't be so boring :) Although I do, obviously, see your point
shylent
@shylent Finding and fixing a bug caused by something like this is *way* more boring! :)
Pekka
Well, I've never said, that your query was wrong or redundant.
shylent
@shylent I know. Just teasing back :)
Pekka
thanks 4 help its working...INTERVAL 15 MINUTE i need to set this as a variable how can i do it?
mayuri
@mayuri You mean as a PHP variable? Just insert it into your query, nothing special to it. As a mySQL field, I'm not sure it can be done.
Pekka
i want the time to be changed dynamically later so i needed
mayuri
@mayuri that is impossible to answer, as you are giving no information about the context you are working in. Check out constants in PHP, maybe they are what you need. http://www.php.net/constants
Pekka
i want to fetch the records that were entered in last 15 mins, from that if the number of failures are more then 5 i want to block further login so i want that time i e 15 minutes to change later so need to make it dynamic..forgive for my english
mayuri
@mayuri Put it in a variable? I don't know what your question is.
Pekka
do you mean $var1=INTERVAL 15 MINUTE its nt correct..
mayuri
can i use between?
mayuri
@PEKKA SELECT status FROM `login_attempts` WHERE lastlogin BETWEEN NOW() AND 1500 1500 is minutes in seconds is this correct can i use this?please help its urgent
mayuri
@mayuri why 1500? What is wrong with INTERVAL 15 MINUTE?
Pekka
my boss want it to be dynamic to change later so.. i dont have any problem...
mayuri
sorry for eating ur time and annoying u..m new to this..
mayuri
@mayuri just store `$variable = "INTERVAL 15 MINUTE";` and use `$variable` in the query. It won't make much sense though as you can change it in the code anyway. The usual way is to define a constant in the top of the file: `define("QUERY_INTERVAL", "15 MINUTE");` and then to use the constant in the query: "SELECT ..... BETWEEN (NOW(), INTERVAL".QUERY_INTERVAL.")");` Constants are explained in the link I posted above.
Pekka
thanks very very its working......
mayuri
+1  A: 
select * from table WHERE datetimeField BETWEEN date_add(NOW(),INTERVAL -15 MINUTE) AND NOW();
Māris Kiseļovs
+1 for using `BETWEEN`, it's the better solution of course!
Pekka