CodeIgniter:如何使括号处于JOIN条件 [英] CodeIgniter: how to have brackets in a JOIN condition

查看:54
本文介绍了CodeIgniter:如何使括号处于JOIN条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如何在CodeIgniter中编写此查询?我想在加入条件中使用方括号。

How to write this query in CodeIgniter? I want to have brackets in the JOIN condition.

CodeIgniter示例

CodeIgniter sample

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

但这会产生错误。

推荐答案

我会使用query()函数并编写普通的SQL。

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

就像这样:

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

使其更具可读性。在大型应用程序中,我们经常让所有查询由专家检查,或者没有CI程序员检查,这在使用活动记录表示法时是不可能的。

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.

您甚至可以将查询放入

括号不是必需的。

这篇关于CodeIgniter:如何使括号处于JOIN条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆