Mysql 重复插入SQL查询,更改一列值

Mysql 重复插入SQL查询,更改一列值,mysql,sql,Mysql,Sql,我有一个表名“emp_records”,其中有如下条目: Point_name | date | value | X | 2016-02-01 | 22.2 | Y | 2016-02-01 | 44 | Z | 2016-02-01 | 10.4 | X | 2016-02-02 | 2.8 | Y | 2016-02-02 | 22

我有一个表名“emp_records”,其中有如下条目:

Point_name |     date      | value  |
X          | 2016-02-01    | 22.2   |
Y          | 2016-02-01    | 44     |
Z          | 2016-02-01    | 10.4   |
X          | 2016-02-02    | 2.8    |
Y          | 2016-02-02    | 22.5   |
Z          | 2016-02-02    | 15.4   |
.
.
.
因此,每个日期都有对应于X,Y,Z的条目。 现在我想做以下事情: 假设今天的日期是:2016-03-15

获取表中具有最新日期的X、Y、Z的条目,即X、Y、Z的最新条目截止日期为2016-02-20

将这些条目从2016-02-20复制到当前日期-1,即2016-03-14。对于每组X、Y、Z,只应更改日期列。 例如,如果最新条目集为2016-02-20:

将此集合复制到当前日期-1 比如:


这只是一个样本表,我用它来提出我的疑问。实际表包含9个不同的点名称和许多其他列。有人可以在一个SQL查询中编写此语句吗?

此语句满足您的要求

insert into @emp_records SELECT point_name,dateadd(d,-1,GETDATE()),value
           FROM @emp_records WHERE DATE IN (SELECT MAX(DATE) FROM @emp_records)
我使用下面的语句来测试它

DECLARE @emp_records TABLE (point_name VARCHAR(5),[DATE] DATE,VALUE DECIMAL(18,2))
INSERT INTO @emp_records VALUES ('X','02/20/2016',20.3)
INSERT INTO @emp_records VALUES ('Y','02/20/2016',10.7)
INSERT INTO @emp_records VALUES ('Z','02/20/2016',12.8)
insert into @emp_records SELECT point_name,dateadd(d,-1,GETDATE()),value
       FROM @emp_records WHERE DATE IN (SELECT MAX(DATE) FROM @emp_records)
SELECT * FROM @emp_records

在MySql中,如果没有生成这些行的表或内联查询,就无法生成动态数量的行。因为你最近的约会可能是在遥远的过去,你可能需要很多这样的记录

所以我建议首先创建一个名为series的helper表。此操作只需执行一次:

create table series (
  n int auto_increment,
  primary key (n)
);

insert into series values (); -- 1 record
insert into series select null from series; -- 2 records
insert into series select null from series; -- 4 records
insert into series select null from series; -- 8 records
insert into series select null from series; -- 16 records
insert into series select null from series; -- 32 records
insert into series select null from series; -- 64 records
insert into series select null from series; -- 128 records
insert into series select null from series; -- 256 records
insert into series select null from series; -- 1024 records
insert into series select null from series; -- 2048 records
您可以重复该插入操作,使该表中的记录数翻倍,直到有足够的记录为止。但2000年已经过去了6年,所以实际上这已经足够了

然后,insert语句变为:

insert into emp_records (point_name, date, value)
    select point_name,
           DATE_ADD(date, INTERVAL  series.n DAY) as date,
           value
    from   emp_records,
           series
    where  date = (select max(date) from emp_records)
    and    DATE_ADD(date, INTERVAL  series.n DAY) <= CURDATE();
是显示所选零件结果的小提琴。

首先获取具有每个点名称的最大日期的emp\u记录。以下查询为您提供表中每个点的MAXDATE记录:

现在,当您获得需要插入的emp_记录时,通过迭代上述结果来构建查询字符串:

/* PSUEDO-CODE (As Server-side Language not tagged) */
/* DECLARE a variable holding the Query-String */
string queryString = "INSERT INTO emp_records (Point_name, date, value) VALUES "; 
/* GET the Current-Date in the language, you are using */
string dateString = "2016-03-15"; 
/* ITERATE the Fetched-Data resulted from the First-Query to Build the INSERT Query */
for(i=0; i<FetchedArray.Length; i++) 
{ 
    /* GET the Days-Difference in dateString and FetchedArray['date'] */
    for(j=1; j <= numberOfDifferenceDays; j++) 
    { 
        /* DECLARE a Variable having the Date tobe inserted named as insertDate */
        insertDate = FetchedArray['date'].AddDays(j); 
        /* CONCATENATE the queryString */
        queryString += "(" +    FetchedArray['Point_name'] + 
                         ", " + insertDate + ", " + 
                         ", " + FetchedArray['value'] + "), ";
    } 
} 
/* 
 AT the end of this Iteration, 
 you would get a INSERT-QUERY that needs to be executed 
 to get what you wanted
 Display variable queryString to check if requirement has fulfilled
 Remove the COMMA at the end of the variable queryString 
 If you have the Correct Insert-Query (Execute it)
*/

您在MySQL中使用哪种语言???[因为在我看来,一个查询不可能做到这一点]!如果你想分两步完成,我可以帮你……@user3078630,有没有任何答案适合你的需要?你能提供一些反馈吗?嘿,舒克里,我不明白这部分:dateaddd,-1,GETDATE
insert into emp_records (point_name, date, value)
    select point_name,
           DATE_ADD(date, INTERVAL  series.n DAY) as date,
           value
    from   emp_records,
           series
    where  date = (select max(date) from emp_records)
    and    DATE_ADD(date, INTERVAL  series.n DAY) <= CURDATE();
SELECT * FROM 
( 
    SELECT 
        Point_name, date, value 
    FROM 
        emp_records 
    ORDER BY 
      Point_name , date DESC 
)   myAlias 
GROUP BY 
    Point_name 
/* PSUEDO-CODE (As Server-side Language not tagged) */
/* DECLARE a variable holding the Query-String */
string queryString = "INSERT INTO emp_records (Point_name, date, value) VALUES "; 
/* GET the Current-Date in the language, you are using */
string dateString = "2016-03-15"; 
/* ITERATE the Fetched-Data resulted from the First-Query to Build the INSERT Query */
for(i=0; i<FetchedArray.Length; i++) 
{ 
    /* GET the Days-Difference in dateString and FetchedArray['date'] */
    for(j=1; j <= numberOfDifferenceDays; j++) 
    { 
        /* DECLARE a Variable having the Date tobe inserted named as insertDate */
        insertDate = FetchedArray['date'].AddDays(j); 
        /* CONCATENATE the queryString */
        queryString += "(" +    FetchedArray['Point_name'] + 
                         ", " + insertDate + ", " + 
                         ", " + FetchedArray['value'] + "), ";
    } 
} 
/* 
 AT the end of this Iteration, 
 you would get a INSERT-QUERY that needs to be executed 
 to get what you wanted
 Display variable queryString to check if requirement has fulfilled
 Remove the COMMA at the end of the variable queryString 
 If you have the Correct Insert-Query (Execute it)
*/