2011-02-01 96 views
0

任何人都可以指导我如何复制一个mysql表到另一个表使用cakephp在同一个数据库请给我例如查询,如果可能的话。如何在cakephp中将mysql表复制到另一个表中?

+0

您应该学会使用mysql命令行客户端。直接运行你的查询,你不需要编写一个程序来执行它,当MySQL带有一个运行查询的程序。 http://dev.mysql.com/doc/refman/5.1/en/insert-select.html – 2011-02-01 11:07:14

回答

2

如果这是一个快速和肮脏的一次复制,我只是遍历源表中的记录,并创造新的目标记录:

$source = $this->Source->find('all'); 
foreach($source as $sRec) 
{ 
    $this->Target->create(); 
    $targetData = array(); 
    $target['Target']['field1'] = $sRec['Source']['field1']; 
    $target['Target']['field2'] = $sRec['Source']['field2']; 
    //etc 
    $this->Target->save($targetData); 
} 

道歉,如果它的车,我刚刚花了很多钱买它。

被编辑以显示有条件的行选择。沿线的一些东西:

$toCopy = array(1,32,71,72,73); 
foreach($toCopy as $anId) 
{ 
    $sRec = $this->Source->read(null,$anId); 
    $this->Target->create(); 
    $targetData = array(); 
    $target['Target']['field1'] = $sRec['Source']['field1']; 
    $target['Target']['field2'] = $sRec['Source']['field2']; 
    //etc 
    $this->Target->save($targetData); 
} 
4
create table new_table like your_table; 
insert into new_table select * from your_table; 

以上将保持指数NEW_TABLE

create table new_table select * from your_table; 

以上不会保留索引。
没有什么可以做的CakePHP,你只需要正确的SQL语法,
,加上相关的连接设置,让PHP连接到MySQL

+0

如果您必须从CakePHP内部完成,那么必须以$ this-> MyModel-> query(' SOME SQL'); – Leo 2011-02-01 09:13:27

+0

正如你所说的,与cakephp没有任何关系。那么我在哪里写这个查询。 – 2011-02-01 10:40:10

相关问题