问题
SELECT field1, field2
FROM table1
JOIN table2 ON (table2.field1=table1.field1 OR table2.field2=table1.field2)
How to write this query in CodeIgniter? I want to have brackets in the JOIN condition.
CodeIgniter sample
$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.
回答1:
I would go with the query() function and write normal 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 much more readable. In large applications we frequently have all the queries checked by experts or none CI-programmers, which is impossible when using active-record notation.
You can then even put the queries in seperate files, for better abstraction and cross-team accessibility.
The brackets are not neccessary.
来源:https://stackoverflow.com/questions/11913343/codeigniter-how-to-have-brackets-in-a-join-condition