CodeIgniter: how to have brackets in a JOIN condition

南楼画角 提交于 2020-01-04 11:01:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!