简体   繁体   English

Codeigniter连接查询失败

[英]Codeigniter join query failing

I am trying to execute this query in codeigniter 2.2. 我试图在codeigniter 2.2中执行此查询。 I read the documentation http://www.codeigniter.com/user_guide/database/results.html . 我阅读了文档http://www.codeigniter.com/user_guide/database/results.html

My controller code is this 我的控制器代码是这样

            $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,
             );
          }

The results that are turned is this: 结果是这样的:

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',
     ),
    )

These are wrong results. 这些是错误的结果。 I was expecting is the merged results of the query. 我期待的是查询的合并结果。 Below is what I get when I run the query in phpmyadmin 以下是我在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 

Now, it would be nice if I could get CI to return the same set of merged data. 现在,如果我可以让CI返回相同的合并数据集,那就太好了。 I can see that is it only returning the table query for immun and CI is not join data from the other table.s I read somewhere that CI was not build to handle complex queries? 我可以看到它是否仅返回针对immun的表查询,并且CI不是从其他表联接数据。 Is that true? 真的吗?

Any ideas how to get the data I need? 有什么想法如何获取我需要的数据吗? Thanks! 谢谢!

You could see the query CI ran in your database. 您可能会看到查询CI在数据库中运行。

Put the following code on the controller that render the page where this query is used: 将以下代码放在呈现该查询所在页面的控制器上:

$this->output->enable_profiler(TRUE);

This way CI will output a profiler in the end of the page with lots of information, including the executed queries needed for rendering the page. 这样,CI将在页面末尾输出带有大量信息的探查器,包括渲染页面所需的已执行查询。 This should help. 这应该有所帮助。

Another hint, you must use alias in case you need to select columns with equal names from different tables. 另一个提示,如果需要从不同的表中选择名称相同的列,则必须使用别名。 CI don't handle it well. 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;      
 }

This is the correct query for the cross join for codeigniter. 这是针对Codeigniter的交叉联接的正确查询。 In my original post, I did not have conditionals. 在我的原始帖子中,我没有条件。 I found it here 我在这里找到

https://ellislab.com/codeIgniter/user-guide/database/active_record.html https://ellislab.com/codeIgniter/user-guide/database/active_record.html

in the section about join. 在有关加入的部分中。 I seen that there was a place for conditions of the join. 我看到有加入条件的地方。 Once I added the conditions. 一旦我添加了条件。 I got the correct result set returned. 我得到了正确的结果集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM