Mysql将具有特定列和unix时间戳的所有行从一个表复制到另一个表

Mysql将具有特定列和unix时间戳的所有行从一个表复制到另一个表

问题描述:

I am currently copying all rows from Table1 to Table2 each day. Table2 is a 7 day archive of what Table1 contains each day. On average there are 150,000 rows to move over each day.

Table1

id | name | category | image | description | link

Table2

id | name | link | date

To do this I use PHP to select all rows in Table1:

SELECT name, category, link FROM Table1

Then I loop through each one and insert it into Table2:

INSERT INTO Table2 SET name = ''.$row["name"].'', link = ''.$row["link"].'', date = time()

time() is stored as an int(11) in the database. Can anybody advise if this is there is a better or more efficient way of doing this?

我目前正在每天从Table1复制所有行到Table2。 表2是Table1每天包含的7天存档。 平均每天要移动150,000行。 p>

Table1 p>

  id | 名字| 类别| 图片| 描述|  link 
  code>  pre> 
 
 

Table2 p>

  id | 名字| 链接|  date 
  code>  pre> 
 
 

为此,我使用PHP选择Table1中的所有行: p>

SELECT name,category, link FROM Table1 code> p>

然后我遍历每一个并将其插入Table2: p>

  INSERT INTO Table2 SET name =  ''。$ row [“name”]。'',link =''。$ row [“link”]。'',date = time()
  code>  pre> 
 
   time() code>作为int(11)存储在数据库中。 任何人都可以建议是否有更好或更有效的方法吗? p> 
  div>

You can use INSERT INTO SELECT rather than looping:

INSERT INTO Table2(name,link,date)
SELECT name, link, UNIX_TIMESTAMP() FROM Table1