如何使用Idiorm联合联接

时间:2014-08-20 17:02:36

标签: sql join union idiorm

我正在使用Idiorm进行SQL查询,需要建立左右连接的联合。

这是我的实际SQL语句,它起作用:

SELECT p.*, a1.*, a2.szFirstName AS a2FirstName, a2.szLastName AS a2LastName, 

a2.szEmail AS a2Email,
a3.szFirstName AS a3FirstName, a3.szLastName AS a3LastName, a3.szEmail AS a3Email
FROM propertySummary p
LEFT OUTER JOIN agents a1 ON p.lAgentID = a1.lAgentID
LEFT OUTER JOIN agents a2 ON p.lAgentID2 = a2.lAgentID
LEFT OUTER JOIN agents a3 ON p.lAgentID3 = a3.lAgentID

UNION

SELECT p.*, a1.*, a2.szFirstName AS a2FirstName, a2.szLastName AS a2LastName, a2.szEmail AS a2Email,
a3.szFirstName AS a3FirstName, a3.szLastName AS a3LastName, a3.szEmail AS a3Email
FROM propertySummary p
RIGHT OUTER JOIN agents a1 ON p.lAgentID = a1.lAgentID
RIGHT OUTER JOIN agents a2 ON p.lAgentID2 = a2.lAgentID
RIGHT OUTER JOIN agents a3 ON p.lAgentID3 = a3.lAgentID

如何使用Idiorm查询重写此内容?我似乎无法在他们的文档中找到它。任何建议将不胜感激。提前谢谢。

链接到Idiorm Documentation

1 个答案:

答案 0 :(得分:0)

来自closed github issue

  

使用raw_query执行此操作。例如:

$sql = <<<SQL
    SELECT a.* FROM table_name_a a
    UNION ALL
    SELECT b.* FROM table_name_b b
SQL;

$recs = ORM::for_table('table_name_here_a')
    ->raw_query($sql)
    ->find_array();