Codeigniter 加入查询失败
Codeigniter join query failing
我正在尝试在 codeigniter 2.2 中执行此查询。我阅读了文档 http://www.codeigniter.com/user_guide/database/results.html。
我的控制器代码是这样的
$query = $this->db->query("SELECT a.id, a.child, a.immune, a.immun_date, b.id, b.fname, b.lname, c.id, c.name
FROM immun a, children b, immun_master c
WHERE a.child = b.id
AND c.id = a.immune
");
$immun = array();
foreach ($query->result()as $row) {
$immun[] = array(
$row->id,
$row->child,
$row->immune,
$row->immun_date,
);
}
翻出来的结果是这样的:
array (
0 =>
array (
0 => '2',
1 => '1001',
2 => '2',
3 => '2011-04-23',
),
1 =>
array (
0 => '3',
1 => '1001',
2 => '3',
3 => '2011-04-30',
),
2 =>
array (
0 => '6',
1 => '1002',
2 => '6',
3 => '2011-04-30',
),
3 =>
array (
0 => '5',
1 => '1002',
2 => '5',
3 => '2011-04-29',
),
4 =>
array (
0 => '1',
1 => '1003',
2 => '1',
3 => '2011-01-06',
),
5 =>
array (
0 => '3',
1 => '1005',
2 => '3',
3 => '2010-10-04',
),
6 =>
array (
0 => '3',
1 => '1231',
2 => '3',
3 => '2014-08-01',
),
)
这些是错误的结果。我期待的是查询的合并结果。以下是我 运行 在 phpmyadmin
中查询时得到的结果
id child immune immun_date id fname lname id name
1 1001 2 2011-04-23 1001 Johny Jame 2 Swine Flu Vaccine
2 1001 3 2011-04-30 1001 Johny Jame 3 Bird Flu Vaccine
3 1002 6 2011-04-30 1002 Chelsea James 6 Hepatitis B
4 1002 5 2011-04-29 1002 Chelsea James 5 Measles Vaccine
5 1003 1 2011-01-06 1003 Charles Jacob 1 H1N1 Vaccine
6 1005 3 2010-10-04 1005 Hansome Little 3 Bird Flu Vaccine
7 1231 3 2014-08-01 1231 Jennifer Ylanan 3 Bird Flu Vaccine
现在,如果我能得到 CI 到 return 同一组合并数据就好了。我可以看到它只是 returning table 查询免疫并且 CI 没有加入来自其他 table.s 的数据 我在某处读到 CI 是不是为了处理复杂的查询而构建的?是真的吗?
关于如何获取我需要的数据有什么想法吗?
谢谢!
您可以在您的数据库中看到查询 CI 运行。
将以下代码放在呈现使用此查询的页面的控制器上:
$this->output->enable_profiler(TRUE);
这样 CI 将在页面末尾输出一个分析器,其中包含大量信息,包括呈现页面所需的已执行查询。
这应该有所帮助。
另一个提示,如果您需要 select 来自不同表的具有相同名称的列,则必须使用别名。 CI不好处理。
function immChild() {
$this->db->select('c.id, c.name, b.id, b.fname, b.lname, a.id, a.child, a.immune, a.immun_date');
$this->db->join('immun_master as c', 'c.id = a.immune','true');
$this->db->join('children as b', 'a.child = b.id', 'true');
$query = $this->db->get('immun as a')->result();
return $query;
}
这是 codeigniter 交叉连接的正确查询。在我原来的 post 中,我没有条件句。我在这里找到了
https://ellislab.com/codeIgniter/user-guide/database/active_record.html
在关于加入的部分。我看到有一个加入条件的地方。一旦我添加了条件。我返回了正确的结果集。
我正在尝试在 codeigniter 2.2 中执行此查询。我阅读了文档 http://www.codeigniter.com/user_guide/database/results.html。
我的控制器代码是这样的
$query = $this->db->query("SELECT a.id, a.child, a.immune, a.immun_date, b.id, b.fname, b.lname, c.id, c.name
FROM immun a, children b, immun_master c
WHERE a.child = b.id
AND c.id = a.immune
");
$immun = array();
foreach ($query->result()as $row) {
$immun[] = array(
$row->id,
$row->child,
$row->immune,
$row->immun_date,
);
}
翻出来的结果是这样的:
array (
0 =>
array (
0 => '2',
1 => '1001',
2 => '2',
3 => '2011-04-23',
),
1 =>
array (
0 => '3',
1 => '1001',
2 => '3',
3 => '2011-04-30',
),
2 =>
array (
0 => '6',
1 => '1002',
2 => '6',
3 => '2011-04-30',
),
3 =>
array (
0 => '5',
1 => '1002',
2 => '5',
3 => '2011-04-29',
),
4 =>
array (
0 => '1',
1 => '1003',
2 => '1',
3 => '2011-01-06',
),
5 =>
array (
0 => '3',
1 => '1005',
2 => '3',
3 => '2010-10-04',
),
6 =>
array (
0 => '3',
1 => '1231',
2 => '3',
3 => '2014-08-01',
),
)
这些是错误的结果。我期待的是查询的合并结果。以下是我 运行 在 phpmyadmin
中查询时得到的结果id child immune immun_date id fname lname id name
1 1001 2 2011-04-23 1001 Johny Jame 2 Swine Flu Vaccine
2 1001 3 2011-04-30 1001 Johny Jame 3 Bird Flu Vaccine
3 1002 6 2011-04-30 1002 Chelsea James 6 Hepatitis B
4 1002 5 2011-04-29 1002 Chelsea James 5 Measles Vaccine
5 1003 1 2011-01-06 1003 Charles Jacob 1 H1N1 Vaccine
6 1005 3 2010-10-04 1005 Hansome Little 3 Bird Flu Vaccine
7 1231 3 2014-08-01 1231 Jennifer Ylanan 3 Bird Flu Vaccine
现在,如果我能得到 CI 到 return 同一组合并数据就好了。我可以看到它只是 returning table 查询免疫并且 CI 没有加入来自其他 table.s 的数据 我在某处读到 CI 是不是为了处理复杂的查询而构建的?是真的吗?
关于如何获取我需要的数据有什么想法吗? 谢谢!
您可以在您的数据库中看到查询 CI 运行。
将以下代码放在呈现使用此查询的页面的控制器上:
$this->output->enable_profiler(TRUE);
这样 CI 将在页面末尾输出一个分析器,其中包含大量信息,包括呈现页面所需的已执行查询。 这应该有所帮助。
另一个提示,如果您需要 select 来自不同表的具有相同名称的列,则必须使用别名。 CI不好处理。
function immChild() {
$this->db->select('c.id, c.name, b.id, b.fname, b.lname, a.id, a.child, a.immune, a.immun_date');
$this->db->join('immun_master as c', 'c.id = a.immune','true');
$this->db->join('children as b', 'a.child = b.id', 'true');
$query = $this->db->get('immun as a')->result();
return $query;
}
这是 codeigniter 交叉连接的正确查询。在我原来的 post 中,我没有条件句。我在这里找到了
https://ellislab.com/codeIgniter/user-guide/database/active_record.html
在关于加入的部分。我看到有一个加入条件的地方。一旦我添加了条件。我返回了正确的结果集。