I have this sql :
SELECT count(*) as nb, data_type, rawValue, createdAt
FROM my_table WHERE data_type in('t','r', 'b')
AND CreatedAt >= DATE_SUB(NOW(), interval 24 HOUR)
AND CreatedAt <= NOW() AND device=27 AND id=462
group by data_type order by createdAt DESC ;
Now I get data like this :
nb data_type rawValue createdAt
20 t test 2018-08-01 07:30:30
20 r test 2018-08-01 08:30:30
For data_type = t I have rows from 2018-08-01 10:10:23 but is getting the first value and not the last by createdAt. Can you help me please ? Thx in advance.
it seems db is mysql,
when you will use aggregate function then selection column should be in group by clause . we know now() is returned todays present date time as you given interval 24 hour so just notice when selected is that records fall in this duration or not,because your query is almost ok except group by clause columns, i puts those column in group by clauses
As you expect 1 row per data_type so i changes like below
select T.nd,T.data_type,A.rawValue,T.createdAt from
(
SELECT count(*) as nb, data_type,
max(createdAt) as createdAt
FROM table WHERE data_type in('t','r', 'b')
AND CreatedAt >= DATE_SUB(NOW(), interval 24 HOUR)
AND CreatedAt <= NOW() AND device=27 AND id=462
group by data_type
) as T left join table A
on T.createdAt=A.createdAt
and T.data_type=A.data_type
where A.data_type in ('t','r', 'b')
AND A.CreatedAt >= DATE_SUB(NOW(), interval 24 HOUR)
AND A.CreatedAt <= NOW() AND device=27 AND id=462
It would be great if you share your tables sample data and expected output
Related
This is my query
WHERE id = 14 AND start_time BETWEEN '2019-10-24 00:00:00' AND '2019-12-12 23:59:59'
ORDER BY created_date LIMIT 0 , 10
When I run this query then it returns me data of this data also. -> 2019-10-23T19:23:41.000Z
Issue: When I pass the 2019-10-24 then why it gives me data of 2019-10-23 date?
Note: start_time has a data type -> datetime in db.
It's not a issue by the way it's correct output.
Try this way
DATE_FORMAT(start_time, "%Y-%m-%d %H:%i:%s") as start_time
Due to diffrent formate, It may confused you.
use less than or equal to '<=' or greater than or equal to '>=' operator instead of BETWEEN.
Use this condition in your query.
WHERE id = 14 AND DATE(start_time) >= DATE('2019-10-24 00:00:00') AND DATE(start_time) <= DATE('2019-12-12 23:59:59') ORDER BY created_date LIMIT 0 , 10
OR
WHERE id = 14 AND DATE(start_time) >= '2019-10-24' AND DATE(start_time) <= '2019-12-12') ORDER BY created_date LIMIT 0 , 10
I need to make statistics page for billing with auto query. For example now is july and query must show count of record for the june and current count of records on current day(only july). Sort of:
"records for the last month - 85, for the current day - 32"
I have table customer and row create_time but it is in unix timestamp. Tried
SELECT COUNT(*) FROM customer WHERE create_time >=
UNIX_TIMESTAMP(DATE_SUB(now(), INTERVAL 1 MONTH))
but its absolutely not what i want.
I would appreciate for any help.
Try this:
SELECT *
FROM ( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(date_format(LAST_DAY(now() - interval 1 month),'%Y-%m-'),'01')
AND create_time < LAST_DAY(now() - interval 1 month )
) AS s1,
( SELECT COUNT(*) AS total_previous_month
FROM customer
WHERE create_time >= concat(extract(year from now()),'-',extract(month from now()),'-01')
AND create_time <= now()
) AS s2
You can do this by adding condition to the WHERE:
create_time >= UNIX_TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 month))
in PHP you can calculate the one month back date
$newDate = strtotime('-1 month');
and use this in query
I have a table with some fields and a timestamp field named timestart.
What I would like to do is select all the records from my table where the field timestart is 21 days from now.
But how can I do this?
you can have this with. if you want exact equals to timestamp. use =
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
you can achive the same by using
SELECT *
FROM table
WHERE date = DATE_ADD(NOW(), INTERVAL 21 DAY)
ORDER BY date DESC
The datediff function seems to meet the bill:
SELECT *
FROM my_table
WHERE DATE_DIFF (timestart, CURRENT_DATE()) >= 21
You can use this:
SELECT *
FROM table
WHERE date >= (NOW() - INTERVAL 21 DAY)
ORDER BY date DESC
LIMIT 20
I have a table
id user Visitor timestamp
13 username abc 2014-01-16 15:01:44
I have to 'Count' total visitors for a 'User' for last seven days group by date(not timestamp)
SELECT count(*) from tableA WHERE user=username GROUPBY __How to do it__ LIMIT for last seven day from today.
If any day no visitor came so, no row would be there so it should show 0.
What would be correct QUERY?
There is no need to GROUP BY resultset, you need to count visits for a week (with unspecified user). Try this:
SELECT
COUNT(*)
FROM
`table`
WHERE
`timestamp` >= (NOW() - INTERVAL 7 DAY);
If you need to track visits for a specified user, then try this:
SELECT
DATE(`timestamp`) as `date`,
COUNT(*) as `count`
FROM
`table`
WHERE
(`timestamp` >= (NOW() - INTERVAL 7 DAY))
AND
(`user` = 'username')
GROUP BY
`date`;
MySQL DATE() function reference.
Try this:
SELECT DATE(a.timestamp), COUNT(*)
FROM tableA a
WHERE a.user='username' AND DATEDIFF(NOW(), DATE(a.timestamp)) <= 7
GROUP BY DATE(a.timestamp);
i think it's work :)
SELECT Count(*)
from table A
WHERE user = username AND DATEDIFF(NOW(),timestamp)<=7
Say I want to SELECT all records between two dates plus one record before and one record after that date? All records are ordered by date.
You could use a union combined with the limit statement. Something like what's below (untested, don't have access to mysql).
(select column from table where datefield > startdate and datefield < stopdate)
union
(select column from table where datefield < startdate order by datefield desc limit 1)
union
(select column from table where datefield > stopdate order by datefield limit 1)
This will give you the next row regardless of where it falls date-wise.
Thanks for syntax fix, ponies.
(select * from t where date < start_date order by date desc limit 1)
union (select * FROM t WHERE date between start_date and end_date)
union (select * from t where date > end_date order by date asc limit 1)
You can use functions to add or subtract values, like this:
select * from table where field1 < ADDDATE( CURTIME() , INTERVAL 1 DAY)
Check this link where there are some examples.
SELECT *
FROM table
WHERE date BETWEEN DATE_ADD(current_date(), INTERAL -1 DAY)
AND DATE_ADD(current_date(), INTERVAL 1 DAY);