如何在当前数据库CodeIgniter中从另一个数据库创建新表

时间:2014-10-11 14:07:52

标签: php mysql database codeigniter

我有两个数据库名称为'Employee',因为它有一个表'users'和第二个数据库是'customer'我想将所有数据从 employee-> users表导入到 customer-> cust_users 表使用codeigniter。我该怎么做?? 客户数据库将是主数据库 我有一个查询

"Create TABLE mytest AS (SELECT * FROM user)"

我添加了配置,如 - >

主要数据库这将是主要客户数据库

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'customer';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

用户数据库配置第二个Emp db

$db['emp']['hostname'] = 'localhost';
$db['Emp']['username'] = 'root';
$db['emp']['password'] = '';
$db['emp']['database'] = 'employee';
$db['emp']['dbdriver'] = 'mysql';
$db['emp']['dbprefix'] = '';
$db['empr']['pconnect'] = TRUE;
$db['emp']['db_debug'] = TRUE;
$db['emp']['cache_on'] = FALSE;
$db['emp']['cachedir'] = '';
$db['emp']['char_set'] = 'utf8';
$db['emp']['dbcollat'] = 'utf8_general_ci';
$db['emp']['swap_pre'] = '';
$db['emp']['autoinit'] = TRUE;
$db['emp']['stricton'] = FALSE;


$this->load->database();
$users_db=$this->load->database('emp', TRUE);
$this->db->query("Create TABLE cust_users AS (SELECT * FROM employee)");

但是这会在员工数据库中创建一个不在客户数据库中的表。我该如何在客户数据库中创建表。

1 个答案:

答案 0 :(得分:1)

您可以通过明确添加它来指定要创建的数据库:

$this->db->query("CREATE TABLE customer_db.cust_users AS (SELECT * FROM employee)");