Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a CROSS JOIN with CodeIgniter's query builder

How can CodeIgniter's query builder methods be used to implement a CROSS JOIN?

I tried this:

$this->db->join('tableTwo as b', '', 'CROSS');
$result = $this->db
    ->get('tableOne as a')
    ->result(); 
like image 997
Maetschl Avatar asked Jul 08 '26 22:07

Maetschl


2 Answers

Solution for cross join in codeigniter:

$this->db->join('tableTwo as b','true');
$result = $this->db->get('tableOne as a')->result(); 
like image 134
Maetschl Avatar answered Jul 10 '26 10:07

Maetschl


In codeigniter 3, You can add cross join query builder by

$this->db->join('tableTwo as b','1=1');                    //true relation
$result = $this->db->get('tableOne as a')->result(); 

Or by passing an array in from method or get methods

$result = $this->db->get(['tableOne as a','tableTwo as b'])->result(); 

or

$result = $this->db->from(['tableOne as a','tableTwo as b'])
get()->result();     //by this method you can add as many table you want to join

or by passing one table in from and other in get method

$result = $this->db->from('tableOne as a')
get('tableTwo as b')->result(); 
like image 28
Zohaib Avatar answered Jul 10 '26 10:07

Zohaib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!