codeigniter中的多个连接查询?

时间:2017-06-20 11:25:12

标签: php codeigniter

function getReceiptData($receipt_id){

    $this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
    student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
    fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
    $this->db->from('fee_collections');
    $this->db->join('class','class_id = fee_collection_class_id');
    $this->db->join('student','student_id = fee_collection_roll_id');
    $this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
    $this->db->join('admin','admin_id = fee_collection_added_by');
    $this->db->where('fee_collection_id',$receipt_id);
    $query = $this->db->get();
    return $query->row();
}

这会产生NULL输出为什么?

4 个答案:

答案 0 :(得分:1)

当您使用联接查询时,请使用如下所示的别名。

$this->db->from('fee_collections AS ac');
$this->db->join('class AS cs','cs.class_id = ac.fee_collection_class_id');

请尝试这个,这将帮助您获得输出。

感谢您的编辑! 在进行同行评审之前,此编辑仅对您可见。 function getReceiptData($ receipt_id){

$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();

}

@Ganesh:请检查我下面提到的查询,根据您共享的db模式,它位于我的本地系统中。

$this->db->select('c.name as cname, s.name as sname,fc.*');
$this->db->from('fee_collections fc');
$this->db->join('class c', 'c.class_id = fc.fee_collection_class_id', 'left');
$this->db->join('student s', 's.student_id = fc.fee_collection_roll_id', 'left');
$this->db->join('fee_particulars fp', 'fp.fee_particular_id = fc.fee_particular_id', 'left');
$this->db->join('admin a', 'a.admin_id = fc.fee_collection_added_by');
$this->db->where('fc.fee_collection_receipt', $receipt_id);
$query = $this->db->get();
return $query->row();

请根据您的表格字段名称更改select()行。

如果您有任何错误,请告诉我。

答案 1 :(得分:0)

function getReceiptData($receipt_id){

    $this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
    student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
    fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
    $this->db->from('fee_collections');
    $this->db->where('fee_collection_id',$receipt_id);
    $this->db->join('class','class_id = fee_collection_class_id');
    $this->db->join('student','student_id = fee_collection_roll_id');
    $this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
    $this->db->join('admin','admin_id = fee_collection_added_by');

    $query = $this->db->get();
    return $query->row();
}

答案 2 :(得分:0)

您没有收到任何 sql错误表示您的查询是正确的,但您没有获得数据意味着您的情况错误

没有看到表结构很难说出确切的原因。通过查看您的代码看起来正确,但您可以通过编写原始SQL查询来检查相同的SQL查询。似乎有些表不符合加入条件。

答案 3 :(得分:0)

function getReceiptData($receipt_id){

    $this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
    student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
    fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
    $this->db->from('fee_collections');
    $this->db->join('class','class_id = fee_collection_class_id', 'left');
    $this->db->join('student','student_id = fee_collection_roll_id', 'left');
    $this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
    $this->db->join('admin','admin_id = fee_collection_added_by');
    $this->db->where('fee_collection_id',$receipt_id);
    $query = $this->db->get();
    return $query->row();
}