2012-10-25 40 views
2

我想根據它們是否匹配給定對中的多對多關係來組合兩個表。我已經知道了SQL語句,我想生產,其在功能上等同於以下內容:在Kohana中加入OR

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)) 
... 

但我想用Kohana中的查詢生成器的一致性生產。問題是我似乎無法找到創建複雜ON查詢的方法。到目前爲止,我的一切是

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

,這產生第一AND序列,但我似乎無法與OR把它在一起。

有什麼建議嗎?

+0

我認爲,你可以在這裏找到答案:http://stackoverflow.com/questions/3286539/kohana- 3-orm-how-to-perform-query-with-2-many-to-many-relationships – dzeno

+0

對不起,但是如何回答這個問題呢?這只是顯示如何做內部連接。 –

回答

0

你應該能夠使用在那裏,而不是在(但不測試):

->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() 
+0

當我有機會時,我將不得不自己測試它,但是,如果我錯了,請糾正我,'JOIN'是否需要使用'ON'子句? –

+0

它取決於連接類型 - http://stackoverflow.com/questions/354070/sql-join-where-clause-vs-on-條款 – matino

+0

啊,我不明白這一點。非常感謝。 –