从今天的日期开始删除超过4年的记录

时间:2019-04-05 16:17:20

标签: mysql date intervals rowdeleting

当结束日期列超过当前日期超过3年时,我想从表中删除记录

有关我已经尝试过的内容,请参见下文

 # Start timer in a separated thread
 def start_timer_gui(self):
    try:
        # Test connection
        if s_conn.test_serial(self) == True:
            th = threading.Thread(target=self.thread_timer_gui,
                                  args=())
            th.start()

...


# Start timer (is in a separated thread)
def thread_timer_gui(self):
    Clock.schedule_interval(partial(self.thread_timer_gui_methods), 1)

@timeit
# Call functions for updating gui from separated thread
def thread_timer_gui_methods(self, dt=0):
    self.update_gui()
    #self.get_measured_vales()

def update_gui(self):
    self.emcstatebar.lbl_local_time.text = "13-03-2019 07:32"
...

我希望从表中删除2016年之前的结果

预先感谢

2 个答案:

答案 0 :(得分:2)

这将删除所有end_date为2016年或更早的内容:

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets import DataTable, TableColumn

# Create data for plot
x = [0, 1]
y = [0, 1]
table_index = [0, 1]

# Create the plot (this will be clicked on)
plot = figure(height = 400, width = 600,
              title = 'Select a point', tools = 'tap')
plot_source = ColumnDataSource(data = dict(x = x, y = y))
renderer = plot.circle('x', 'y', source = plot_source, size = 30)

# Create two sets of data for the tablet
master_data = {}
master_data[0] = {'animals': ['dog', 'cat', 'cow', 'mouse'],
                   'plants': ['carrot', 'catnip', 'grass', 'cheese']}
master_data[1] = {'animals': ['elephant', 'lion', 'monkey', 'emu'],
                  'plants': ['grass', 'turnips', 'banana', 'petunias']}

# Create a table
data = master_data[0]
table_source = ColumnDataSource(data)
columns = [  TableColumn(field = 'animals', title = 'Animal'),
             TableColumn(field = 'plants', title = 'Plant')   ]
data_table = DataTable(source = table_source, columns = columns,
                       width = 400, height = 600)

# Here the reactions of the server are defined
def my_tap_handler(attr, old, new):
    index = new[0]
    data_table.source = ColumnDataSource(master_data[index])

plot_source.selected.on_change("indices", my_tap_handler)

# Collect it all together in the current doc
curdoc().add_root(column(plot, data_table))
curdoc().title = 'Select experiment'

编辑:如果要删除end_date超过3年的所有内容:

DELETE FROM membership WHERE YEAR(end_date) <= YEAR(CURDATE() - INTERVAL 3 YEAR);

答案 1 :(得分:0)

一些注意事项:

  • 将裸列end_date与一个值(作为日期类型)进行比较
  • 在运行DELETE之前
  • 在SELECT语句中测试表达式

大于要删除的行上的end_date的第一个日期值是多少?在SELECT语句中测试该表达式。

SELECT DATE_FORMAT( NOW(), '%Y-01-01') + INTERVAL -3 YEAR

返回

2016-01-01

或日期值,距现在四年前

SELECT DATE(NOW()) + INTERVAL -4 YEAR

返回:

2015-04-05

调整该表达式,直到它返回我们需要的值。然后,我们可以将该表达式包含在语句中,与裸列end_date列进行比较,如下所示:

SELECT m.* 
  FROM membership m 
 WHERE m.end_date < DATE(NOW()) + INTERVAL -4 YEAR
 ORDER BY m.end_date DESC

或者如果我们知道我们需要的日期值为'2015-04-05''2017-01-01',那么我们可以将其指定为文字:

SELECT m.* 
  FROM membership m 
 WHERE m.end_date < '2017-01-01' + INTERVAL 0 MONTH
 ORDER BY m.end_date DESC

在我们确认SELECT语句返回了要删除的行集之后,我们可以将SELECT关键字替换为DELETE