CodeIgniter Forums

Full Version: How do i use left join in code igniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

Hi, can i ask..i am still newbie on this code igniter and i am worry about how to use the left join,subquery etcc...can you please show me how to use in codeigniter.

this is my pdo sqlquery how do i convert this to codeigniter sqlquery...
for example like this....
$db->prepare("select emp.empno,emp.firstname,jb.empno,jb.jobname
                             from employee emp LEFT OUTER JOIN job jb
                             on emp.empno = job.empno");


please help me thannk you in advance.Smile

El Forum

I work with CodeIgniter 3.0 dev, and the following code should work:

$query = $this->db
    ->select('emp.empno, emp.firstname, jb.jobname')
    ->from('employee AS emp')
    ->join('job AS jb', 'emp.empno = job.empno', 'left outer')
    ->get(); // See in the manual how to deal with query results.

As a result it builds the following raw query:

SELECT `emp`.`empno`, `emp`.`firstname`, `jb`.`jobname`
FROM `employee` AS `emp`
LEFT OUTER JOIN `job` AS `jb` ON `emp`.`empno` = `job`.`empno`

It is strange to me why the field jb.empno is placed, it is repetitive, I think. I removed it.

For lower version of CodeIgniter I did not test this. You may see this.

El Forum

Not mandatory, but useful:

For development purpose you may enable the profiler, it displays useful information at the bottom of the page. I shows the generated raw queries, so you can make comparison whether they are correct.

For production system disable the profiler.

El Forum

Hi Thank you for the quick reply...and thank you for helping me.