CodeIgniter: how to have parentheses in a JOIN state

SELECT field1, field2
FROM table1
JOIN table2 ON (table2.field1=table1.field1 OR table2.field2=table1.field2)

      

How do I write this request in CodeIgniter? I want to have brackets in a JOIN state.

CodeIgniter example

$this->db->select("field1,field2")
$this->db->from("table1")
$this->db->join("table2","(table2.field1=table1.field1 or table2.field2=table1.field2)")

      

But this generates an error.

+2


source to share


1 answer


I would go with the query () function and write plain SQL.

Like this:

$this->db->query(
   'SELECT field1, field2 
    FROM table1
    JOIN table2 
        ON table2.field1=table1.field1 
        OR table2.field2=table1.field2');

      



Makes it more readable. In large applications, we often have all the queries reviewed by experts or no CI programmer, which is not possible with active notation notes.

You can then put queries in separate files to better abstract away and have cross-command access.

The parentheses are optional.

-1


source







All Articles