使用symfony2在createQuery中使用UNION

时间:2013-08-05 08:02:53

标签: sql symfony union createquery

我正在尝试

$query = $em->createQuery(
            'SELECT id, name, beginDate, endDate FROM SmartguideEntityBundle:Promotion1 UNION SELECT id, name, beginDate, endDate FROM SmartguideEntityBundle:Promotion2'
        );

但结果是

Error: Expected end of string, got 'SELECT'

我该如何解决?

2 个答案:

答案 0 :(得分:1)

尝试定义别名,如

SELECT p1.id, 
p1.name, 
p1.beginDate, 
p1.endDate 
FROM SmartguideEntityBundle:Promotion1 p1 
UNION 
SELECT p2.id, 
p2.name, 
p2.beginDate, 
p2.endDate 
FROM SmartguideEntityBundle:Promotion2 p2

编辑:如果您查看此链接,ti会显示如何使用 RAW SQL SQL query with UNION in Doctrine Symfony)执行查询

答案 1 :(得分:0)

B