Query to limit number of requests per day - mysql

I am trying to limit the user to perform only 5 requests per day. To do that, I am counting the number of request already performed during the same day using the query below:
SELECT COUNT(*) FROM table_name WHERE date_field >= CURDATE()
This gives me requests already performed days before. All I need is only the number of request performed in the course today. Any idea please?

SELECT COUNT(*) FROM table_name WHERE date_field >= CURDATE()
... will return the number of rows where date_field is greater than or equal to today if date_field is a date datatype. That is, ensure time is not a component but it certainly cannot return "requests already performed days before" unless there is a bug in the value stored in date_field column.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Try:
SELECT COUNT(*) FROM table_name WHERE date(date_field) = CURDATE()
Assuming date_field is a timestamp/datetime field, date() will extract just the date part and evaluate it against the current date. You can read more about it on MySQL's Documentation

Related

How to compare a date when the date is stored as a varchar

The dates in my database are stored as varchars instead of date formats due to the way it was first built.
The dates look like this:
e.g. 1/3/2015 and
10/3/2015
I'm trying:
"SELECT COUNT(*) n FROM tracker WHERE TIMESTAMP(STR_TO_DATE(date, '%d/%m/%Y'))<=NOW()"
However, that's not working. It is returning the count of all records, regardless of the date.
How can I count only the records where the date is today or in the past?
You do not need TIMESTAMP():
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= NOW()
You should pay attention to the functions STR_TO_DATE and NOW(), the first return a date, the second is a timestamp.
When you convert STR_TO_DATE(date, '%d/%m/%Y') you will get a date with hours, minutes and seconds as 00:00:00
Using CURRENT_DATE perhaps will match more closely the original requirements
SELECT COUNT(*) as n
FROM tracker
WHERE STR_TO_DATE(date, '%d/%m/%Y') <= CURRENT_DATE
Also I suggest you to rename the column 'date'

Grabbing date records that are exactly a month or more away

I put together a query for determining whether the time difference between the current date and a record from a database is exactly a month or more apart. I am comparing now() to a created_at column, which is a timestamp.
EX:
6-12-2014,
7-12-2014
AND
5-12-2014,
7-12-2014
Should be considered to be a desirable results.
SELECT count(*) FROM `subscriptions` WHERE
DATE_ADD(CAST(created_at as DATE),INTERVAL TIMESTAMPDIFF(MONTH, created_at, now()) MONTH) = CAST(now() as DATE);
However the query appears to not return all desired results. It returns 2-28-2014 and 7-28-2014, however it does not pull up 6-28-2014. Is there a better way of doing this than the solution I came up with?
Are you looking to count dates that are on the same day of the month as the current date? If so, try the DAYOFMONTH function:
SELECT COUNT(*)
FROM subscriptions
WHERE DAYOFMONTH(created_at) = DAYOFMONTH(NOW())

Calculate difference between dates

The title might be a bit misleading, but what I want is:
SELECT * FROM table ORDER BY pid ASC
And in one of the columns I have a DATE(). I want to compare the current date (not time) and return how many days are left till that date. Let's say the date is 2013-04-20 and today's date is 2013-04-16 I don't want to get any data if it's < current date. If it is I want it returned in days.
I've been looking around here and I've found no way to do it, and I can't for the love of me figure it out.
If you're looking for the difference between two date you can use the GETDATE function in MS SQL
SELECT DATEDIFF(DD, DateOne, DateTwo) FROM TABLE
This will return the difference in number of days between the two dates.
If you only want rows where the date field is less than or equal to today's date you can use:
SELECT DATEDIFF(DD, DateField, GETDATE())
FROM TableName
WHERE DateField <= GETDATE()
If you're using MySQL you can use DATEDIFF()
SELECT
DATEDIFF(NOW(), date_column) AS days_diff
FROM
tablename
Get the difference between two dates (ANSI SQL)
select the_date_column - current_date as days_left
from the_table
where the_date_column - current_date <= 4;
SQLFiddle: http://sqlfiddle.com/#!12/3148d/1

Grouping timestamp field by date

I am trying to write an SQL query to return how many links were submitted to my website over the last 7 day period. So far I have this:
SELECT COUNT(`id`) AS `count`
FROM `links`
WHERE `created` > NOW() - 86400
AND `created` < NOW()
this works for one day, it returns one row called count with the number of links submitted in the last 24 hours. I need to change it to return 2 columns called date and count, with 7 rows (one for each day).
The tricky part that I can't get my head around is that created is a timestamp column, and I don't have access to change it so I have to work with it.
Edit: work in progress for the query:
SELECT DAY(FROM_UNIXTIME(created)) AS day, COUNT(id) count
FROM links
GROUP BY DAY(FROM_UNIXTIME(created))
LIMIT 7
NOW() actually shouldn't be working as it returns a datetime. Also, if you want to fetch 7 days worth of data, you want to subtract 604800 from UNIX_TIMESTAMP(). You can use then date and time functions with FROM_UNIXTIME. This will make grouping easier. Optimally, your column should be of datetime type.
It would go something like:
SELECT DAY(FROM_UNIXTIME(created)) day, COUNT(id) count
FROM links
WHERE created > UNIX_TIMESTAMP() - 604800 AND created < UNIX_TIMESTAMP()
GROUP BY DAY(FROM_UNIXTIME(created))
You can alternatively use the BETWEEN operator:
WHERE created BETWEEN UNIX_TIMESTAMP() - 604800 AND UNIX_TIMESTAMP()
See the demo

Need to Select DateTime Range in MySQL

I have a txn (transaction) table which has a datetime field named post_ts, which is the field that represents the date of the transaction.
I am writing the code for a batch job (which is supposed to be a weekly job to be run on Sundays) using Spring Batch, and I need to run the query such that I need to fetch all transactions from a week back to the date I am running the job.
Apart from other million business rules to be applied to the query, I need the transactions from (currentdate) to (current date - 7)
You can user mysql DATEDIFF function to achieve this :
SELECT *
FROM txn
WHERE DATEDIFF( CURDATE( ) , `post_ts` ) <= 7;
If you don't need more precision that the date part (no hours, minutes nor seconds to be taken into account), then you can use the function DATEDIFF:
SELECT *
FROM txn
WHERE DATEDIFF( NOW(), post_ts ) <= 7;
EDIT: I've changed the answer. It was mistaken, as I first used the TIMEDIFF function instead of DATEDIFF.
TIMEDIFF gives the difference in hours, minutes, ... whereas DATEDIFF gives the difference in the date part, ignoring the hour:minutes:seconds part.
select
*
from
txn
where
TO_DAYS(NOW()) - TO_DAYS(post_ts) <= 7;