sql - 如何在Doctrine 2.0中编写UNION

标签 sql union doctrine-orm

如何在Doctrine 2.0中编写此SQL查询(并获取结果)?

(SELECT 'group' AS type, 
    CONCAT(u.firstname, " ", u.surname) as fullname, 
    g.name AS subject,
    user_id, 
    who_id, 
    group_id AS subject_id,
    created 
  FROM group_notification 
  JOIN users u ON(who_id = u.id) 
  JOIN groups g ON(group_id = g.id)
)

   UNION 

(SELECT 'event' AS type, 
    CONCAT(u.firstname, " ", u.surname) as fullname, 
    e.name AS subject, 
    user_id, 
    who_id, 
    event_id AS subject_id, 
    created 
  FROM event_notification 
  JOIN users u ON(who_id = u.id) 
  JOIN events e ON(event_id = e.id)
)
   ORDER BY created

最佳答案

好吧,我发现也许是最好的解决方案:

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"group" = "NotificationGroup", "event" = "NotificationEvent"})
 */
class Notification {
   // ...
}


然后两个类(NotificationGroup和NotificationEvent)扩展了Notification:

/**
 * @Entity
 */
class NotificationGroup extends Notification {
    //...
}

/**
 * @Entity
 */
class NotificationEvent extends Notification {
    //...
}

关于sql - 如何在Doctrine 2.0中编写UNION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4155288/

相关文章:

SQL 连接主键上的行值

一张表的 MySQL UNION 查询 + ORDER BY

php - Symfony crud 生成的索引 View ,其中没有引用字段

php - 原则 2 - 从实体外部禁用 PrePersist

mysql - Min, MAX SQL 查询优化

mysql - MySQL 支持 STORAGE 语法吗?

sql - 如何在Oracle中仅选择值变化?

mysql - UNION ALL mysql 子句中的行默认顺序?..我的意思是首先获取哪一行?

MySQL 试图同时使用 INNER JOIN 和 UNION?

doctrine-orm - 查询生成器 : Search a value in a column containing comma-separated integers