在CodeIgniter中加入查询

时间:2013-03-05 07:43:16

标签: php sql codeigniter join

我在CodeIgniter中使用连接查询,但无法使其工作。它只显示一个表数据,但不显示另一个。我是CodeIgniter的新手,无法解决这个问题。请求有人帮助我。 Tnanks提前。

查看

<?php foreach ($query as $row): ?>

    <?php echo $row->answerA;?><br>
    <?php echo $row->answerB;?><br>
    <?php echo $row->answerC;?><br>
    <?php echo $row->comment;?><br>
    <?php echo $row->name; ?>

<?php endforeach; ?>  

控制器

function getall() {

    $this->load->model('result_model');
    $data['query'] = $this->result_model->result_getall();
    // print_r($data['query']);
    // die();
    $this->load->view('result_view', $data);

}

模型

function result_getall() {

    $this->db->select('tblanswers.*,credentials.*');
    $this->db->from('tblanswers');
    $this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'right outer'); 
    $query = $this->db->get();
    return $query->result();

}

修改

的结果
print_r($data['query']);
die();

是一个数组如下:

Array ( [0] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 83 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => ledvinas@yahoo.ie ) [1] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 84 [name] => Edvinas [second_name] => liutvaits [phone] => [email] => ledvinas@yahoo.ie ) [2] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 85 [name] => Edvinas [second_name] => Liutvaitis [phone] => [email] => ledvinas@yahoo.ie ) [3] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 86 [name] => EdvinasQ [second_name] => LiutvaitisQ [phone] => 12345678 [email] => ledvinas@yahoo.ie ) [4] => stdClass Object ( [answerid] => [userid] => [questionid] => [answerA] => [answerB] => [answerC] => [comment] => [cid] => 87 [name] => Edvinas [second_name] => Liutvaitis [phone] => 123456 [email] => ledvinas@yahoo.ie ) ) 

表格结构

凭证

cid(PRIMARY),姓名,second_name,电话,电子邮件

tblanswers

answerid(PRIMARY),userid,questionid,answerA,answerB,answerC。

4 个答案:

答案 0 :(得分:11)

试试这个:

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid');

或者

$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'inner');

打印结果以查看是否是您想要的

答案 1 :(得分:2)

删除select()标记,因为你需要所有术语。

你可以修改像

这样的代码
$this->db->join('credentials', 'tblanswers.answerid = credentials.cid', 'outer'); 
$query = $this->db->get('tblanswers');
return $query->result();

答案 2 :(得分:1)

您要查询的主表是什么?那么你可以试试这个,建议在使用连接时,你必须为每个表指定一个别名:

 function result_getall(){

    // if you want to query your primary data from the table 'tblanswers',
    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('tblanswers a');
    $this->db->join('credentials b', 'b.cid = a.answerid', 'left'); 
    $query = $this->db->get();
    return $query->result();

    // if you want to query your primary data from the table 'credentials',

    $this->db->select('a.*,b.*'); <-- select what you might want to select
    $this->db->from('credentials a');
    $this->db->join('tblanswers b', 'b.answerid = a.cid', 'left'); 
    $query = $this->db->get();
    return $query->result();

}

答案 3 :(得分:0)

如果你的两个表都有相关的,那么它应该有效。你为什么使用右外连接? 只需使用内部联接或任何其他联接,它将起作用。请在此处阅读http://www.w3schools.com/sql/sql_join.asp和此处http://dev.mysql.com/doc/refman/5.0/en/join.html

的不同类型的加入

希望这会有所帮助。

谢谢