2017-07-20 85 views
0

我的表像这样的每一行不同:插入行是为插入

CREATE TABLE test(id INT,product_id INT,sent_at datetime); 
CREATE TABLE products(id INT); 

我想建立这样的查询:

INSERT INTO test(product_id,sent_at) 
    SELECT products.id as product_id, 
    DATE_ADD(NOW(),INTERVAL 10 MINUTE) as sent_at 
    FROM products; 

我在products表中有1000行,我希望test表中每行的sent_at列的值不同。 对于插入的products表中的每一行,sent_at列应该多增加10分钟。 我不希望test表中所有行的sent_at列的值相同。

而且我不能使用程序。我正在使用MySQL 5.7。

请帮帮我。提前致谢。

回答

0

在MySQL verywell工作

mysql> select * from products; 
+------+ 
| id | 
+------+ 
| 23 | 
+------+ 
1 row in set (0.00 sec) 

mysql> INSERT INTO test(product_id,sent_at) 
    ->  SELECT products.id as product_id, 
    ->  DATE_ADD(NOW(),INTERVAL 10 MINUTE) as sent_at 
    ->  FROM products; 
Query OK, 1 row affected (0.00 sec) 
Records: 1 Duplicates: 0 Warnings: 0 

mysql> select * from test; 
+------+------------+---------------------+ 
| id | product_id | sent_at    | 
+------+------------+---------------------+ 
| NULL |   23 | 2017-07-20 19:26:36 | 
+------+------------+---------------------+ 
1 row in set (0.00 sec)