Javascript自动计数30天并从表中删除数据

时间:2019-04-02 05:25:17

标签: javascript mysql node.js html-framework-7

mysql table structure screenshot

我想让表格数据从当前日期算起30天后删除。

上面是mysql数据库结构的屏幕截图(我使用当前时间戳作为日期)

这是我用来从客户表中选择所有数据的代码


$$(document).on('page:init', '.page[data-name="home_employee"]', function(e) {

    console.log('this is page employee');

    var sqlget = 'SELECT * FROM `customers`';

    //this is select the data from customers [emplotee_table (home_employee.html)]
    connection.query(sqlget, function(error, results, fields) {


        for (i = 0; i < results.length; i++) {

            $$(document).find('#employee_table').append($$('<tr data-id="' + results[i].id + '"> ' +
                '<td class="checkbox-cell">' +
                '<label class="checkbox">' +
                '<input type="checkbox">' +
                '<i class="icon-checkbox"></i>' +
                '</label>' +
                '</td>' +
                ' <td style="font-family:Poppins-Medium;" class="label-cell"> ' + results[i].name + ' </td>' +
                ' <td class="numeric-cell">' + results[i].telephone + '</td> ' +
                ' <td class="numeric-cell">' + results[i].order_num + '</td> ' +
                '<td class="numeric-cell">' + results[i].juice1_num + '</td> ' +
                '<td class="numeric-cell">' + results[i].juice2_num + '</td> ' +
                '<td class="numeric-cell">' + results[i].juice3_num + '</td> ' +
                '<td class="numeric-cell">' + results[i].date + '</td> ' +
                '</tr>'));
        }
    });

});

table screenshot at here

我如何使javascript计算时间戳?还是我需要更改mysql结构才能完成此任务?

1 个答案:

答案 0 :(得分:0)

我通过一些技巧完成了

var sqldeletetime = "DELETE FROM customers WHERE date < DATE_SUB(now(), INTERVAL 30 DAY)";

    connection.query(sqldeletetime, function(err, result) {
        if (err) throw err;
        console.log("delete every record pass 30 days");
    });