简体   繁体   English

与OR一起加入Kohana

[英]Joining with OR in Kohana

I'm trying to combine two tables based on if they match a given pair in a many-to-many relation. 我正在尝试根据两个表是否在多对多关系中匹配给定对来进行组合。 I already know the SQL statement I'm trying to produce, which is functionally equivalent to the following: 我已经知道我要生成的SQL语句,该语句在功能上等效于以下语句:

SELECT columnA, columnB, ...
...
JOIN matching_table
    ON ( (matching_table.id1 = table_a.id AND matching_table.id2 = table_b.id) OR
         (matching_table.id1 = table_b.id AND matching_table.id2 = table_a.id) )
...

But I want to produce it using Kohana's query builder for consistency. 但是我想使用Kohana的查询生成器来生成它以保持一致性。 The problem is I can't seem to find a way of creating a complex ON query. 问题是我似乎找不到找到创建复杂的ON查询的方法。 So far all I've got is 到目前为止,我所拥有的只是

DB::select('columnA', 'columnB', ...)
...
    ->join('matching_table')
        ->on('matching_table.id1', '=', 'table_a.id')
        ->on('matching_table.id2', '=', 'table_b.id')
...

and this generates the first AND sequence but I can't seem to put it together with an OR . 这会生成第一个AND序列,但我似乎无法将其与OR放在一起。

Any suggestions? 有什么建议么?

You should be able to use where instead of on (not tested though): 您应该可以使用where而不是on(尽管未测试):

->join('matching_table')
->where_open()
->where('matching_table.id1', '=', 'table_a.id')
->and_where('matching_table.id2', '=', 'table_b.id')
->where_close()
->or_where_open()
->where('matching_table.id1', '=', 'table_b.id')
->and_where('matching_table.id2', '=', 'table_a.id')
->or_where_close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM