如何根据日期条件(PHP-MySQL-Query)将多个行从表插入另一个表?

时间:2014-03-21 21:16:51

标签: php mysql

我在原理图中看到了两张桌子。我想要复制URI中三列fieldsdetailstable1的数据,如果table2中的日期更长,则将其插入table1比12/11/2013。像下面的查询:

INSERT INTO table2 (all_links, fields_one, fields_two) FROM table1 (URI, fields, details) WHERE date>"12-11-2013 00-00-00";

您能帮忙解决这个问题吗?

schematic picture

3 个答案:

答案 0 :(得分:2)

喜欢

INSERT INTO table2 (all_links, fields_one, fields_two) 
select URI, fields, details FROM table1 
WHERE date > "12-11-2013 00-00-00";

如果表位于不同的DB中(假设dbo是默认所有者)

INSERT INTO DB1.dbo.table2 (all_links, fields_one, fields_two) 
select URI, fields, details FROM DB2.dbo.table1 
WHERE date > "12-11-2013 00-00-00";

答案 1 :(得分:1)

尝试这样的事情:

   INSERT INTO tbl_temp2 (fld_id)
      SELECT tbl_temp1.fld_order_id
      FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;

并查看文档:

http://dev.mysql.com/doc/refman/5.1/en/insert-select.html

答案 2 :(得分:1)

INSERT INTO table2 (all_links, fields_one, fields_two) select URI, fields, details from table1 where date>"12-11-2013 00-00-00";