On a previous question about how I could limit the amount of times a form is submitted in an hour, someone said this:
select count(*) from mysql_table where uid='$uid' and timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR););
That seem's like it would work, but I have no idea how. First I'd like to replace uid with IP:
select count(*) from mysql_table where ip='$ip' and timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR););
But after that I'm not quite sure how the timestamp > (DATE_ADD(now(), INTERVAL - 1 HOUR);); actually works. I do submit a timestamp with each post, but I don't know how the rest actually works, can someone explain it to me?
Your query counts how many records are present in "mysql_table"
select count(*) from mysql_table
and limit that count to records having ip=user_ip
where ip='$ip'
and timestamp (the date/time column in which recording time is stored) is greater than time representing "one hour ago"
timestamp > (DATE_ADD(now(), INTERVAL -1 HOUR)
(remember that every time you insert a record, you probably set timestamp field to current date/time).
What you have to do is comparing this result with maximum allowed and decide if it's good or not.
Related
Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.
I have a mysql(i) databse that is written to every minute (usually 4 or 5 seconds after the minute).
I would like to select the values that are cleset to top-of-the-hour for the last 36 hours and I've no idea how to do it.
I've been playing with INTERVAL and DATE_ADD but have not found something that works yet. Any help would be appreciated.
Edit:
Extra info:
Table name:
temperature
Column names:
uid (AI)
time (timestamp)
probe0
probe1
probe2
probe3
probe4
Perhaps it would also be better to be
now
now -1hour
now -2hours
etc
now -36hours
FWIW, I'm currently using the following code so select ALL the data for the last 36 hours (2160 rows)
SELECT time, probe0, probe1, probe2, probe3, probe4 FROM temperature WHERE temperature.time >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE) ORDER BY temperature.time DESC
If you want to get the first record for each hour you can use MySQL's MIN() function and since it is an Aggregate Function you need to use GROUP BY to group your data by date and hour. So a sample query will look like:
SELECT
MIN(`time`) as first_for_hour, probe0, probe1, probe2, probe3, probe4
FROM `temperature`
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
GROUP BY DATE(`time`), HOUR(`time`);
and for reading data 36 hours back I used your WHERE clause:
WHERE `time` >= DATE_ADD(NOW(), INTERVAL -2160 MINUTE)
I hope this will be helpful to you and here is the playground.
Need help here, having an mysql table called APPROVAL, there having an id,dateandtime and level, i need a query that selects the id alone with the following condition.
Taking date alone from database and comparing it with current system date, if the days exceeds above 30 and below 60 and also level = 5.
How can I write a query for this.
Thanks in advance.
MySQL has good date arithmetic. For example, the expression
CURDATE() + INTERVAL 30 DAY
gives a datetime value denoting midnight 30 days hence. Similarly
CURDATE() + INTERVAL 61 DAY
yields midnight on the 61st day.
So a query of the form
SELECT ID
FROM APPROVAL
WHERE Level = 5
AND `DateTime` >= CURDATE() + INTERVAL 30 DAY
AND `DateTime` < CURDATE() + INTERVAL 61 DAY
will yield what you want. Notice the use of >= for the beginning of the range of days, and the use of < and an extra day for the end of the range. We do that because we want all items from the 60th day, and none from the 61st day.
A compound index on (Level, DateTime) will make this query very efficient to satisfy.
Notice that an expression like
DATE(`DateTime`) <= CURDATE() + INTERVAL 60 DAY /* slow! */
will also yield correct results, but the presence of the the DATE() function call on the column to be searched makes it unsargeable. That is, it makes MySQL unable to use an index to satisfy the search.
Ok so use this query to retrieve all the IDs that match level 5 and date diff between 30 and 60 compared to the current date.
SELECT id
FROM APPROVAL
WHERE level = 5 && DATEDIFF(NOW(), dateandtime) BETWEEN 30 AND 60
I'd suggest you to order them dy date DESC too.
Hope that helps
I hope, I understood your problem correctly.
select `ID`
from APPROVAL
where `Level` = 5
and ( DATE(`DateTime`) > curdate() + interval 30 day
and DATE(`DateTime`) < curdate() + interval 60 day )
order by `ID` asc;
Where DATE() gets the date from a datetime and CURDATE() is the current system date. With interval you can manipulate a date expression whitout having to worry about its limits.
When the authentication code in the Tuser is not null it means the users didnt login the site within 24 hours so i have to delete the rows.
Here is the query which I wrote but it is not deleting correctly
DELETE FROM tusers WHERE auth_code IS NOT NULL
AND auth_code !=''
AND STR_TO_DATE(NOW(),'%Y-%m-%d %T')
> (DATE_SUB(STR_TO_DATE(created_date,'%Y-%m-%d %T'),INTERVAL -1 DAY))
You are in a better position to determine whether it is right or not by testing and letting us know if it works or not.
But I can tell you that it is more complicated than it needs to be. You can compare dates directly like this:
... where date(now()) > date_sub(created_date, interval 1 day)
Note that if you are going to SUBTRACT a day in order to go back, you should subtract a positive number of days. Subtracting a negative number of days refers to later in time, not earlier.
I got the perfect delete which is working perfectly:
DELETE FROM tusers WHERE auth_code IS NOT NULL AND auth_code !=''
AND DATE_SUB(NOW() , INTERVAL 24 HOUR) > STR_TO_DATE(created_date,'%Y-%m-%d %T');
This query deletes the rows by comparing the current date of the system with the created date of the rows which reaches above 24 hours of the current date.
Basically this query should select all the fields inserted in the last 30 minutes, but it doesnt, it selects absolutely every row making my script output wrong data
SELECT count(*) FROM mytable
WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
My time field stores the time in this kind of format 2011-06-08 22:32:03
The query works, but it selects every row, not the ones inserted in the last 30 minutes.
Try
SELECT count(*) FROM mytable
WHERE `time` >= DATE_SUB(NOW(), INTERVAL 30 minute)
Add parentheses to your query - UTC_TIMESTAMP() or use NOW() - quite simpler for me.