Consider a table named "Books" every one hour new entry will be created in UTC format. I want to fetch last 24 hours data (24 entries) in my local timezone.
I tried this
select * from books where created_at >= DATE_SUB(NOW(),INTERVAL 24 HOUR);
How do I fetch records at runtime for the past 24 hours without setting timezone in mysql? Also the created data comes under three different id's, i need the data belonging to id=1.
(EDITED) Try this :
select * from books
where DAYOFMONTH(DATE(created_at))<>DAYOFMONTH(NOW())
order by DATE(created_at) desc
limit 24;
Now If you want to change the Time zone, for a session use this query :
SET GLOBAL TIME_ZONE = 'ASIA/CALCUTTA'
But the only disadvantage is you have to write it every time you restart MySQL.
To change it permananently, Put the following in your mysql server configuration (e.g. my.cnf):
default-time-zone=ASIA/CALCUTTA
And the only last last option left is conversion in query itself :
select convert_tz(created_at,'UTC', 'ASIA/CALCUTTA ') MyTimeZone from books
where DAYOFMONTH(DATE(created_at))<>DAYOFMONTH(NOW())
order by MyTimeZone desc
limit 24;
Try this:
SELECT * FROM books WHERE created_at > DATE_SUB(NOW(),INTERVAL 24 HOUR);
I wanted to comment on Sagar Joon answer but I cannot so I am adding separate one:
select * from books
where DATE(created_at) < date(now())
order by DATE(created_at) desc
limit 24;
Related
I am trying to get tweet counts for the last ten minutes of a game
assuming I have a sample table as below
id 2 starttime 2005-11-10 16:30:00 endtime 2005-11-10 18:22:
then I have a tweettable as below
tweet id 28215441122544 created 2005-11-10 18:13:43
.
tweet id 25889966555552 created 2005-11-10 18:14:15
how would I write this as a query to count the tweet that occurred within the last ten mins of the sample table. I have tried all sorts, am new to SQL and hive, any help is appreciated.
Are you looking for something like this:
SELECT * FROM myTable
WHERE created >= NOW() - INTERVAL 10 MINUTE
EDIT: if you need enddate from your other table, you will need to use something like that:
SELECT * FROM myTable
WHERE created >= (select endtime from othertable where id=2) - INTERVAL 10 MINUTE
In the question, we are not provided the info that how to connect the tables by using Id column (that's why Id=3 hardcoded here), but the OP knows I believe.
This should give you the number of tweets (rows) in the last ten minutes
SELECT COUNT(*)
FROM sampleTable
WHERE create > date_sub(sysdate(), interval 10 minute)
I have a table called 'Articles' in that table I have 2 columns that will be essential in creating the query I want to create. The first column is the dateStamp column which is a datetime type column. The second column is the Counter column which is an int(255) column. The Counter column technically holds the views for that particular field.
I am trying to create a query that will generate the last 30 days of records. It will then order the records based on most viewed. This query will only pick up 10 records. The current query I have is this:
SELECT *
FROM Articles
WHERE DATEDIFF(day, dateStamp, getdate()) BETWEEN 0 and 30
LIMIT 10
) TOP10
ORDER BY Counter DESC
This query is not displaying any records, but I don't understand what I am doing wrong. Any suggestions?
The MySQL version of the query would look like this:
SELECT a.*
FROM Articles a
WHERE a.dateStamp >= CURDATE() - interval 30 day
ORDER BY a.counter DESC
LIMIT 10;
Your query is generating an error. You should look at that error before fixing the query.
The query would look different in SQL Server.
I have two fields, "cont_time_published" and "cont_date_published" that I want to use to filter results in a listing of records.
select *
from news
WHERE cont_date_published < CURDATE() AND cont_time_published < CURTIME()
I am trying to achieve listing that only shows records that the publish time and publish date is in the past. So that it would filter a record that has today's date but the time is still in the future.
Results are wrong when the date is today and the time is future.
cont_date_published is "DATE" only field and cont_time_published is "TIME" Field.
If you need to get the records published in the past (today in the past hours or before) try:
SELECT *
FROM news
WHERE cont_date_published < CURDATE()
OR (cont_date_published = CURDATE() AND cont_time_published < CURTIME());
sorry my mistake, forgot the OR
Try this
SELECT *
FROM news
WHERE (cont_date_published < CURDATE() ) AND ( cont_time_published < CURTIME() )
ORDER BY id DESC;
You can CAST to an actual date:
WHERE CAST(CONCAT(cont_date_published, ' ', cont_time_published) AS DATETIME)<NOW()
... but this will possibly prevent query optimiser from using indexes (if any). An alternative would be:
WHERE cont_date_published<CURDATE() OR
(cont_date_published=CURDATE() AND cont_time_published<CURTIME())
Of course, all this extra work could be easily avoided with a proper database design that makes use of a single DATETIME column:
WHERE cont_published<NOW()
select * from news WHERE cont_date_published < CURDATE()
This will be enough to get the records that are Published in the Past.
According to this documentation from my understanding using INTERVAL 8 DAY will return any records greater than 8 days.
In my statement $moztimestampnow is the current date in this format 2015-05-21 and moztimestamp pertains to the column in the DB that contains the other earlier date in which I need to calculate with.
I am not sure if I am able to use moztimestamp as the column name in this statement and it is not working.
How do I get the difference in days?
$moztimestampnow = date('Y-m-d');
SELECT *,DATEDIFF('$moztimestampnow',moztimestamp) INTERVAL 8 DAYS FROM backlinks WHERE user_id = '$user_id' LIMIT 10
First, you a misinterpreting the documentation. The interval keyword is for adding values to dates. If you want to filter data, you need to use the where clause.
In your case, the best where clause looks like this:
SELECT bl.*, DATEDIFF('$moztimestampnow', moztimestamp)
FROM backlinks bl
WHERE user_id = '$user_id' and
moztimestamp <= DATE_SUB(CURDATE(), INTERVAL 8 DAY)
LIMIT 10
This can take advantage of an index on backlinks(user_id, moztimestamp). In addition, you probably should have an ORDER BY clause. That is expected when using LIMIT.
Your syntax doesn't make sense. Try something like:
SELECT *
FROM backlinks
WHERE DATE_SUB(moztimestamp, INTERVAL 8 DAY) > '$moztimestampnow'
AND user_id = '$user_id'
LIMIT 10
I don't follow your objective, so you may have to change the order and direction in the first WHERE clause.
I'm making a fitness logbook where indoor rowers can log there results.
To make it interesting and motivating I'm implementing an achievement system.
I like to have an achievement that if someone rows more than 90 times within 24 weeks they get that achievement.
Does anybody have some hints in how i can implement this in MYSQL.
The mysql-table for the logbook is pretty straightforward: id, userid, date (timestamp),etc (rest is omitted because it doesn't really matter)
The jist is that the first rowdate and the last one can't exceed the 24 weeks.
I assume from your application that you want the most recent 24 weeks.
In mysql, you do this as:
select lb.userid
from logbook lb
where datediff(now(), lb.date) >= 7*24
group by userid
having count(*) >= 90
If you need it for an arbitrary 24-week period, can you modify the question?
Just do a sql query to count the number of rows a user has between now and 24 weeks ago. This is a pretty straight forward query to run.
Look at using something with datediff in mysql to get the difference between now and 24 weeks ago.
After you have a script set up to do this, set up a cron job to run either every day or every week and do some automation on this.
I think you should create a table achievers which you populate with the achievers of each day.
You can set a recurrent(daily, right before midnight) event in which you run a query like this:
delete from achievers;
insert into achievers (
select userid
from logbook
where date < currenttimestamp and date > currenttimestamp - 24weeks
group by userid
having count(*) >= 90
)
For events in mysql: http://dev.mysql.com/doc/refman/5.1/en/events-overview.html
This query will give you the list of users total activity in 24 weeks
select * from table groupby userid where `date` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 168 DAY ) AND CURDATE( ) having count(id) >= 90