0

How can i convert this sql to codeigniters active record format

SELECT COUNT(*)
FROM(
SELECT DISTINCT component
FROM `multiple_sample_assay_abc`
WHERE labref = 'NDQA201303001'
)x

I have tried this function but it has sql error

 function getAssayMultipleCount($labref){
        $this->db->count_all('*');
        $this->db->from();
        $this->db->distinct('component');
        $this->db->from('multiple_sample_assay_abc');
        $this->db->where('labref',$labref);
        $query=  $this->db->get();
        return $result=$query->result();
        //print_r($result);
    }
alphy
  • 811
  • 4
  • 13
  • 22

2 Answers2

0

you are using 2 $this->db->from() statements.

phirschybar
  • 7,685
  • 12
  • 46
  • 62
0

Try this

function getAssayMultipleCount($labref){
    $this->db->distinct('component');
    $this->db->from('multiple_sample_assay_abc');
    $this->db->where('labref',$labref);
    $query=  $this->db->get();
    $result=$query->result();
    return count($result);
    //print_r($result);
}

You can just count the no of elements after the query is executed. You don't have to use sub query for this

Bhuvan Rikka
  • 2,655
  • 1
  • 14
  • 27