[英]How can I get the month that is not yet updated in SQL by inserting another row on every update?
我有一个表,其中包含需要每月更新的不同事务的记录。 成功更新了特定月份的记录后,它将在该表中插入一条新记录以指示该记录已被更新。 让我们来看这个例子。
**date_of_transaction** **type**
2015-04-21 1 //A deposit record
2015-04-24 2 //A withdrawal record
2015-04-29 1
2015-04-30 2
2015-04-30 3 //3, means an update record
2015-05-14 1
2015-05-22 1
2015-05-27 2
2015-05-30 2
2015-06-09 1
2015-06-12 2
2015-06-17 2
2015-06-19 2
假设今天是2015年7月23日,我只能获得比当前月份低一个月的数据,因此,我只能获得六月和向下的记录。
如您所见,由于type属性中的值为“ 3”,因此在4月进行了一次更新,但是在5月和6月没有发生任何更新,我如何获取不存在的月份尚未更新?
我的方法是先查找所有月份,然后查找记录已更新的月份。 然后,从所有未更新记录的月份中仅选择那些月份(A减去操作)。
MySQL查询将是这样的
从your_table_name中选择extract(MONTH,data_of_transaction),月份不在其中(从type = 3的表中选择extract(MONTH,data_of_transaction));
这将返回您几个月,没有type = 3行
SELECT MONTH([trans-date]) FROM [table] GROUP BY MONTH([trans-date]) HAVING MAX([trans-type])<3
注意:如果列中的3不是最大值,这将不起作用
您可以尝试一下;
select *
from tbl
where date_of_transaction < 'July 23, 2015'
and
date_format(date_of_transaction, '%M-%Y') in (
select
date_format(date_of_transaction, '%M-%Y')
from tbl
group by date_format(date_of_transaction, '%M-%Y')
having max(type) != 3
)
date_format(date_of_transaction, '%M-%Y')
将考虑month-year
,并过滤type = 3
的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.