https://www.tutorialspoint.com/mysql-query-to-delete-all-rows-older-than-30-days
dbsa@dbsa-X406UA:~$ ssh root@172.16.10.244
root@172.16.10.244's password:
Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-31-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Thu Aug 13 13:37:13 WIB 2020
System load: 0.1 Processes: 131
Usage of /home: 73.7% of 26.80GB Users logged in: 2
Memory usage: 11% IP address for eth0: 172.16.10.244
Swap usage: 0%
Graph this data and manage this system at:
https://landscape.canonical.com/
Last login: Thu Aug 13 13:37:13 2020 from 172.17.10.182
root@ITOP-DEV:~# mysql -u root -p
mysql> use dbitop_yg
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> SHOW TABLES;
mysql> delete from ticket where Start_date < now() - interval 180 DAY;
Query OK, 392857 rows affected (2 min 5.64 sec)
ATAU
mysql> DELETE FROM ticket WHERE Start_date < '2019-12-30 08:21:22';
Query OK, 372410 rows affected (1 min 54.35 sec)
mysql>
-------------------------------------------------------------------------------------------------
To delete all rows older than 30 days, you need to use the DELETE with INTERVAL. Use < now() i.e. less than operator to get all the records before the current date.
Let us first create a table −
mysql> create table DemoTable -> ( -> UserMessage text, -> UserMessageSentDate date -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Hi','2019-06-01'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('Hello','2019-07-02'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Awesome','2019-05-04'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values('Good','2019-01-10'); Query OK, 1 row affected (0.35 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------------+---------------------+ | UserMessage | UserMessageSentDate | +-------------+---------------------+ | Hi | 2019-06-01 | | Hello | 2019-07-02 | | Awesome | 2019-05-04 | | Good | 2019-01-10 | +-------------+---------------------+ 4 rows in set (0.00 sec)
Following is the query to delete all rows older than 30 days −
mysql> delete from DemoTable where UserMessageSentDate < now() - interval 30 DAY; Query OK, 3 rows affected (0.11 sec)
Let us check table records once again −
mysql> select *from DemoTable;
Output
+-------------+---------------------+ | UserMessage | UserMessageSentDate | +-------------+---------------------+ | Hello | 2019-07-02 | +-------------+---------------------+ 1 row in set (0.00 sec)
DELETE FROM table WHERE date < '2011-09-21 08:21:22';
Ini membantu saya menghapus data berdasarkan atribut yang berbeda. Ini berbahaya jadi pastikan Anda membuat cadangan database atau tabel sebelum melakukannya:
mysqldump -h hotsname -u username -p password database_name > backup_folder/backup_filename.txt
Sekarang Anda dapat melakukan operasi hapus:
delete from table_name where column_name < DATE_SUB(NOW() , INTERVAL 1 DAY)
Ini akan menghapus semua data dari sebelum satu hari. Untuk menghapus data sebelum 6 bulan:
delete from table_name where column_name < DATE_SUB(NOW() , INTERVAL 6 MONTH)
Untuk menunjukkan hasil hingga kemarin
WHERE DATE(date_time) < CURDATE()
Untuk menampilkan hasil 10 hari
WHERE date_time < NOW() - INTERVAL 10 DAY
Untuk menampilkan hasil sebelum 10 hari
WHERE DATE(date_time) < DATE(NOW() - INTERVAL 10 DAY)
Ini akan bekerja untuk Anda
Anda dapat menemukan tanggal seperti ini
SELECT DATE(NOW() - INTERVAL 11 DAY)
Tidak ada komentar:
Posting Komentar