我的查询有问题,我需要连接来自不同数据库的两个表现在我的问题是如何执行我的查询。我从这里得到了我的语法格式
请先访问此链接,以便您了解为什么我的 SQL 语法是这样的
http://www.x-developer.com/php-scripts/sql-connecting-multiple-databases-in-a-single -查询
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
$SELECT = "SELECT $ACCOUNTS.BALANCES_TABLE.IDNO, $ACCOUNTS.BALANCES_TABLE.balance";
$FROM = "FROM $ACCOUNTS.BALANCES_TABLE";
$WHERE = "$ACCOUNTS.BALANCES_TABLE.IDNO IN (SELECT $ENROLLEES.ENROLLEES_TABLE.IDNO FROM $ENROLLEES.ENROLLEES_TABLE)";
$SQL = $SELECT ." ". $FROM ." ". $WHERE;
主要问题:如何执行我的查询?
如果我们在 codeIgniter 中这样做:
$ENROLLEES->query($SQL); or $ACCOUNTS->query($SQL);
如何执行我有多个数据库的查询?我会在这里提供什么
[database]->query($SQL);
?
$sql="Select * from my_table where 1";
$query = $this->db->query($sql);
return $query->result_array();
如果数据库共享服务器,则拥有对这两个数据库都具有权限的登录名,并且只需运行类似于以下内容的查询:
$query = $this->db->query("
SELECT t1.*, t2.id
FROM `database1`.`table1` AS t1, `database2`.`table2` AS t2
");
否则我认为你可能必须分别运行两个查询并随后修复逻辑。
我可以看到@Þaw提到的内容:
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
CodeIgniter 支持多种数据库。您需要像上面那样将两个数据库引用保留在单独的变量中。到目前为止你是对的/正确的。
接下来您需要按如下方式使用它们:
$ENROLLEES->query();
$ENROLLEES->result();
和
$ACCOUNTS->query();
$ACCOUNTS->result();
而不是使用
$this->db->query();
$this->db->result();
请参阅此供参考: http://ellislab.com/codeigniter/user-guide/database/connecting.html
http://www.bsourcecode.com/codeigniter/codeigniter-select-query/
$query = $this->db->query("select * from tbl_user");
或
$query = $this->db->select("*");
$this->db->from('table_name');
$query=$this->db->get();
return $this->db->select('(CASE
enter code hereWHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
WHEN orderdetails.DealID = 0 THEN products.name
END) as product_name')
$this->db->select('id, 名称, 价格, 作者, 类别, 语言, ISBN, 发布日期');
$this->db->from('tbl_books');
Select Query:
$this->db->select('column1, column2');
$this->db->from('your_table');
$query = $this->db->get();
Insert Query:
$data = array('column1' => 'value1','column2' => 'value2');
$this->db->insert('your_table', $data);
Update Query:
$data = array('column1' => 'new_value1','column2' => 'new_value2');
$this->db->where('your_condition');
$this->db->update('your_table', $data);
Delete Query:
$this->db->where('your_condition');
$this->db->delete('your_table');