I have a table with 5 million DATETIME records. I want to add a year to all the various datetimes in the table. Can I do it all with a single query? Something like:
SELECT DATE_ADD(*, INTERVAL 1 YEAR);
Or any other way you would recommend. Thanks!
This should do what you want:
UPDATE table SET datefield = DATE_ADD(datefield, INTERVAL 1 YEAR);
If you need to update every table in the database check the answers to this question
Related
I have two date column in a table, one for date of question posted and another for answer. For example whenever a new question is asked its date is stored in q_date column and whenever some answer that question its date is stored in a_date column.
Now I want all the question asked within one week and all the questions which are answered within one week.
Can anyone explain me a query by using join or subquery
You can try the following query:
SELECT *
FROM yourTable
WHERE
q_date >= DATE(NOW()) - INTERVAL 7 DAY OR
a_date >= DATE(NOW()) - INTERVAL 7 DAY
This will return all record corresponding to questions or answers happening with the last 7 days.
You can use a query like this:
Select * from q_ans where DATEDIFF(WEEK,q_ans.q_date,q_ans.a_date) = 1
This will give all records for which answer date is in one week range of question date
You need to add AND condition for BETWEEN clause. You can try
WHERE q_ans.a_date BETWEEN date_sub( NOW(), INTERVAL 1 WEEK) AND NOW()
We are using MySQL, and the table have a column named 'createdat` which means the time when the record are inserted.
Now we want to get data within 10 days from its created time.
Something like this:
select xx from table where time_of_now - createdat<= 10
I wonder if MySQL support this kind of query?
You can use DATEDIFF to get the difference between 2 dates in days, like so:
select xx
from table
where datediff(now(),createdat) <= 10
Documentation
select xx from table where createdat >= CURDATE() - INTERVAL 10 day
I have a lot of old data. And I want to delete it.
Data example:
id ,title, date
What is the best way to delete old data using SQL? I mean delete row if too old, for example 1 month. How can I compare row's date with current time?
PS. I have date column. Look at the example.
Try sommething like this:-
DELETE FROM myTable WHERE [columndate]< DATE_SUB(NOW(), INTERVAL 1 MONTH);
If there isn't any row which stores when was the data inserted then it cannot be specified.
If there is a row named createddate,
you can try
delete from mytable where DATEDIFF(createddate, now())>30
OR you can try with
delete from mytable where createddate< DATE_SUB(NOW(), INTERVAL 1 MONTH);
DELETE * FROM TableName WHERE date < DATE();
I have table tbl_dtcount. In that table there is one column for date.
Now I need to reduce one day for each and every rows in that date field. The date is beginning from 2012-05-19 to 2012-07-03. What is the MySQL update statement to perform this?
How about this.
Update tbl_dtcount
set mydate = DATE_SUB(mydate, INTERVAL 1 DAY)
where <conditions>;
UPDATE table_name
SET date_column = DATE_SUB('1998-01-02', INTERVAL 1 DAY)
....
see detail MySQL DATE_SUB
I inserted a number of rows 3 hours ago and I don't want these rows to change. How can I write a sql statement that will compare current time with the timestamp in the row and restrict users from changing it if above criteria is met.
Thanks
If you want to do it by using mysql, you will have to use the INTERVAL statement which will allow you to "add" time to date functions... for instance:
UPDATE table
SET data = 'whatever'
WHERE NOW() - INTERVAL 3 HOUR < last_change
You can find more info and examples here: Date and Time Functions
You can use a WHERE clause in all updates:
UPDATE yourtable
SET foo = bar
WHERE inserttime > NOW() - interval 3 hour