简体   繁体   English

如何在上个月的记录中添加1并将其作为当前月份的记录插入?

[英]How to add 1 to the previous month's record and insert it as a current month record?

My table structure is like below 我的表结构如下

id     month      year  dummyval
11     January    2017  1
12     January    2017  1
13     January    2017  2

I am inserting next month to this table but I also want to insert a dummyval for each id. 我将在下个月向该表插入数据,但我也想为每个ID插入一个dummyval。 Like the table should be- 就像桌子应该是-

id     month       year  dummyval
11     January     2017  1
12     January     2017  1
13     January     2017  2
11     February    2017  2
12     February    2017  2
13     February    2017  3

How to do that? 怎么做? Please help. 请帮忙。

Try this: 尝试这个:

insert into table1 select id , 'February', 2017, dummyval+1 from table1  
where id=11;

with each insert you do.. 每做一次插入..

Here's an example: 这是一个例子:

mysql> select * from K3;
+------+------+
| name | val  |
+------+------+
| x    | 1    |
| y    | 2    |
+------+------+

INSERTING: 插入:

insert into K3 select 'g', val+1 from K3 where name='x';

AFTER INSERT: 插入后:

mysql> select * from K3;
+------+------+
| name | val  |
+------+------+
| x    | 1    |
| y    | 2    |
| g    | 2    |
+------+------+

You can see that it has inserted 'g' with 2, incremented from x's 1 您会看到它已插入'g'与2,从x的1开始递增

Since you store the month name in your code, you need to have a way to find the next month name. 由于您将月份名称存储在代码中,因此需要找到下一个月份名称的方法。 To keep it simple (but somewhat lengthy), I would begin by defining an auxiliary data structure to hold basic month information: 为了使其简单(但有些冗长),我将首先定义一个辅助数据结构来保存基本月份信息:

create table months (monthid int, monthname varchar(20));
insert into months (monthid, monthname)
select 1, 'January' union all
select 2, 'February' union all
select 3, 'March' union all
/*... all other months*/
select 12, 'December';

Suppose now that your max year-month value in your table (call it 'data') is 2017/January. 现在假设您表中的年度(月份)最大值(称为“数据”)为2017年/ 1月。 You could use the following code to find the next year-month-dummyval combination and insert it into your table, without having to know beforehand which records you need to insert (and their ids): 您可以使用以下代码查找下一个year-month-dummyval组合并将其插入到表中,而无需事先知道需要插入哪些记录(及其ID):

/* a temporary table to hold "next month" info. It should be avoided using a 
join, but I failed miserably in my attempt! */
create temporary table next_month (monthid int, monthname varchar(20), next_id int, next_name varchar(20));

insert into next_month (monthid, monthname, next_id)
select monthid, monthname, case when monthid = 12 then 1 else monthid + 1 end
from months;

update next_month set next_name = 
 (select monthname from months where months.monthid = next_month.next_id);

Now you are ready to insert your new data: 现在您可以插入新数据了:

insert into data (month, year, dummyval)
select 
  next_month.next_name, 
  case when months.monthid = 12 then data.year + 1 else data.year end,
  data.dummyval + 1
from data join months on data.month = months.monthname 
join next_month on months.monthid = next_month.monthid
where data.month = 'January' and data.year = 2017;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM