Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在CodeIgniter中创建“视图(SQL)”并从中选择数据?_Php_Sql_Codeigniter_Ms Access_Sql View - Fatal编程技术网

Php 如何在CodeIgniter中创建“视图(SQL)”并从中选择数据?

Php 如何在CodeIgniter中创建“视图(SQL)”并从中选择数据?,php,sql,codeigniter,ms-access,sql-view,Php,Sql,Codeigniter,Ms Access,Sql View,我有一个查询,需要先检查记录是否仍处于活动状态,然后再将其包含在查询中。现在我的问题是,该记录的记录状态在另一个数据库中,我们都知道不能连接来自不同数据库的表 我要做的是从另一个数据库创建一个视图,然后将该视图加入到我的查询中。问题是如何在CodeIgniter中创建视图并从中选择数据 提前谢谢 顺便说一下,我不是设计数据库的人-公司定义- 这是我的一个查询示例,它不是精确的,因为它包含很多表。我希望我能给你一个暗示,告诉你我想做什么 SELECT count(IDNO), course, su

我有一个查询,需要先检查记录是否仍处于活动状态,然后再将其包含在查询中。现在我的问题是,该记录的记录状态在另一个数据库中,我们都知道不能连接来自不同数据库的表

我要做的是从另一个数据库创建一个视图,然后将该视图加入到我的查询中。问题是如何在CodeIgniter中创建视图并从中选择数据

提前谢谢

顺便说一下,我不是设计数据库的人-公司定义-

这是我的一个查询示例,它不是精确的,因为它包含很多表。我希望我能给你一个暗示,告诉你我想做什么

SELECT count(IDNO), course, sum(student_balance)
FROM student_balances
WHERE school_term = '2013' AND student_balance > 0
GROUP BY course
ORDER BY course
所有学生记录都被选中,无论其是否已注册。有一个包含当前学年注册学生的表,该表来自另一个数据库。我只想数一数注册学生的记录

我们都知道不能连接来自不同数据库的表

不确定是否适用于您的情况,但以下是一些关于跨数据库查询的帖子:

无论如何,您不需要使用联接;只需查询另一个数据库,看看它是否处于活动状态

$DB2 = $this->load->database('otherdb', TRUE);
$active = $DB2->query('SELECT is_active blah...');
if($active)
{
    //do other query
}
使现代化 这可能在语法上不正确,但应该为您指明正确的方向。一如既往


我有点困惑,视图是否需要在单独的数据库上创建并传输到另一个数据库?@Paw Cabelin-自从您添加了查询细节后,我更新了我的答案。
// load other db
$db2 = $this->load->db('otherdb',TRUE);

// get enrolled student id's from other db
$active_students = $db2->query('SELECT id FROM students WHERE enrolled = 1')->result();

// query this db for what you want
$this->db->select('count(IDNO), course, sum(student_balance)');
$this->db->where('school_term',2013);
$this->db->where('student_balance >',0);

// where_in will limit the query to the id's in $active_students
$this->db->where_in('id', $active_students);

// finally, execute the query on the student_balances table
$balances = $this->db->get('student_balances');