MY query looks like this:
SELECT COUNT(entryID)
FROM table
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Will this count the rows whose date values are within the day (starting at 12:00; not within 24 hours)? If not, how do I do so?
The following should be enough to get records within the current day:
SELECT COUNT(entryID)
FROM table
WHERE date >= CURDATE()
As Michael notes in the comments, it looks at all records within the last two days in its current form.
The >= operator is only necessary if date is actually a datetime - if it's just a date type, = should suffice.
Here's the solution:
SELECT COUNT(entryID)
FROM table
WHERE DATE(date) >= CURDATE()
Since my date column is type DATETIME, I use DATE(date) to just get the date part, not the time part.
CURDATE() returns a date like '2012-03-30', not a timestamp like '2012-03-30 21:38:17'. The subtraction of one day also returns just a date, not a timestamp. If you want to think of a date as a timestamp think of it as the beginning of that day, meaning a time of '00:00:00'.
And this is the reason, why this
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 1 DAY)
and this
WHERE date > CURDATE()
do the same.
I have another hint: SELECT COUNT(entryID) and SELECT COUNT(*) give the same result. SELECT COUNT(*) gives the database-machine more posibilities to optimize counting, so COUNT(*) is often (not always) faster than COUNT(field).
Related
I have query that counts the number of distinct values of a given field for a given week that where not recorder before. It counts the number of new values for the week.
SELECT
COUNT(DISTINCT (field))
FROM
table
WHERE
(creation BETWEEN CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY AND CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) - 2 DAY)
AND field NOT IN (SELECT DISTINCT
(field)
FROM
table
WHERE
creation < CURDATE() - INTERVAL DAYOFWEEK(CURDATE()) + 5 DAY);
I need to extend the query to be able to do the same computation for all the weeks in the past two months.
How can I do it?
Well, if I understand this correctly, a field occurs in some week for the first time. You want to count per week how many such first occurrences exist. So get the first date per field first. Then count per week.
select
yearweek(first_occurence),
count(*)
from
(
select field, min(creation) as first_occurence
from table
group by field
) fields
group by yearweek(first_occurence)
order by yearweek(first_occurence);
I have tried to filter records but with the use of now function as given below
select * from table where date>= DATE_SUB( NOW( ) ,INTERVAL 90 DAY )
What I need is a select statement that can filter its records for a week or month from the current date but without using NOW() function
if you are using java you could make use of the following code
String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
or you could use curdate() of mysql
Since I found it hard to understand the question I provide the following possibilities:
Try for all dates in a week from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 WEEK)
and for all dates in a month from now:
SELECT * FROM table
WHERE date BETWEEN CURDATE() AND DATE_ADD(CURDATE() ,INTERVAL 1 MONTH)
If you are looking for all dates of the current month use
SELECT * FROM table
WHERE MONTH(date)=MONTH(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
or for all dates of the current week use
SELECT * FROM table
WHERE WEEK(date)=WEEK(CURDATE()) AND YEAR(date)=YEAR(CURDATE())
I have a table structure that looks like this:
I have a perfectly working query that counts how many records there have been per day the last 30 days. It looks likes this:
SELECT DATE(timestamp) AS date, COUNT(id) AS emails FROM 'emails WHERE timestamp >= now() - interval 1 month GROUP BY DATE(timestamp)
This outputs the following which is perfectly fine:
However, the next thing seems too difficult for me to imagine. Now I want to count how many records there have been per day the last 30 days BUT only where newsletter = 1.
I've tried to put a WHERE statement looking like this:
SELECT DATE(timestamp) AS date, COUNT(*) AS emails, nyhedsbrev FROM emails WHERE timestamp >= now() - interval 1 month AND nyhedsbrev = 1 GROUP BY DATE(timestamp)
... And that outputs the following:
The problem is, that its omitting the records with newsletter = 0 and there by I cant compare my first query against the new one, as the dates doesnt match. I know that is because I use WHERE newsletter = 1.
In stead of omitting the record I want a query that just puts a "0" from that date. How can I do this? The final query should be outputting this:
You should be able to simply use SUM() and IF() to get the desired output:
SELECT
DATE(timestamp) AS date,
COUNT(*) AS emails,
SUM(IF(nyhedsbrev > 0, 1, 0)) as nyhedsbrev_count
FROM
emails
WHERE
timestamp >= now() - interval 1 month
GROUP BY
DATE(timestamp)
SQLFiddle DEMO
Edit: You might even be able to simplify it, since it's a boolean, and simply use SUM(nyhedsbrev), but this REQUIRES that nyhedsbrev is only 0 or 1:
SELECT
DATE(timestamp) AS date,
COUNT(*) AS emails,
SUM(nyhedsbrev) as nyhedsbrev_count
FROM
emails
WHERE
timestamp >= now() - interval 1 month
GROUP BY
DATE(timestamp)
Possibly best to get a list of the dates and then left join that against sub queries to get the counts you require.
Something like this
SELECT Sub1.date, Sub2.emails, IFNULL(Sub3.emails, 0)
FROM (SELECT DISTINCT DATE(timestamp) AS date
FROM emails
WHERE timestamp >= now() - interval 1 month) Sub1
LEFT OUTER JOIN (SELECT DATE(timestamp) AS date, COUNT(id) AS emails
FROM emails WHERE timestamp >= now() - interval 1 month
GROUP BY DATE(timestamp)) Sub2
ON Sub2.date = Sub3.date
LEFT OUTER JOIN (SELECT DATE(timestamp) AS date, COUNT(*) AS emails
FROM emails
WHERE timestamp >= now() - interval 1 month AND nyhedsbrev = 1
GROUP BY DATE(timestamp)) Sub3
ON Sub1.date = Sub3.date
(you can probably optimise one subselect of this away, but I have done it in full to make it obvious how it is working)
Assuming newsletter is boolean 1/0 values then this might give you the table that you want:
SELECT DATE(timestamp) AS date, COUNT(*) AS emails, nyhedsbrev
FROM emails WHERE timestamp >= now() - interval 1 month GROUP BY DATE(timestamp),nyhedsbrev ;
Just adding another GROUP BY parameter.
I'm selecting all rows except those from last 24hs. Instead I'd like to select those which are not from today. I'm already getting the day from a datetime column using DATE().
How can I change the query to do it?
select Date, DATE(Date) AS Day
WHERE Date < DATE_SUB(CURDATE(),INTERVAL 1 DAY)
It's easier than you think:
SELECT Date, DATE(Date) AS Day
WHERE Date < CURDATE()
OP said:
I'd like to select those which are not from today
bfavaretto's solution:
SELECT Date, DATE(Date) AS Day
WHERE Date < CURDATE()
would answer the question:
I'd like to select those which date is before today
To properly answer OP's question this query should be run (assuming date is a datetime because of the OP's cast, I'm casting it to a date field, but the cast is unnecesary if Date is actually a date field):
SELECT Date, DATE(Date) AS Day FROM aTable
WHERE Date(date) != CURDATE()
I always have trouble with complicated SQL queries.
This is what I have
$query = '
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND active = 1
ORDER BY date_time ASC
LIMIT 6
';
I want to get up to 6 rows that are upcoming within the hour. Is my query wrong? It does not seem to get events that are upcoming within the next hour when I test it.
What is the correct syntax for this?
I'm going to postulate that you're looking at a group of records that contain a range of DATETIME values, so you probably want something more like this:
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time < DATE_ADD(NOW(), INTERVAL 1 HOUR)
AND date_time >= NOW()
AND active = 1
ORDER BY date_time ASC
LIMIT 6
Otherwise, your query is looking for records with a date_time of exactly "now + 1 hour". I'm assuming all your dates aren't specific to that particular second. ;)
To clarify a bit, DATE_ADD() and DATE_SUB() return exact timestamps, so your query above roughly translates to something like SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., which I don't think is what you want.
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
means date_time equals exactly now minus one hour, which would result in any record exactly one hour old.
Why not use
WHERE TIMEDIFF(date_time, NOW()) < '01:00:00'
AND date_time > NOW()