我是symfony和Doctrine 2的新手
我有一个SQL查询
SELECT DATE_FORMAT(DATE(history.played_at), '%m-%Y') AS 'date' ,
COUNT(DISTINCT(l.id)) AS 'value'
FROM listeners AS l LEFT JOIN history ON history.listener_id = l.id
WHERE history.played_at IS NOT NULL
GROUP BY YEAR(DATE(history.played_at)), WEEK(DATE(history.played_at))
我想在使用Doctrine 2和QueryBuilder的symfony项目中使用它,但是我找不到怎么做
我目前有这个
$qb->select($qb->expr()->countDistinct('l.id'))
->from('AppBundle:Listener', 'l')
->leftJoin('AppBundle:History', 'h', 'l.id = h.listener')
->where($qb->expr()->isNotNull('h.played_at'));
但它根本不起作用,很多东西都缺失了
如果有人能告诉我如何做到这一点,真的很好
答案 0 :(得分:0)
我不是SQL专家,但此查询应该有效:
$qb = $this->em->createQueryBuilder()
->select('COUNT(DISTINCT l.id)')
->from('AppBundle:Listener', 'l')
->leftJoin('AppBundle:History', 'h')
->where('h.playet_at IS NOT null');
如果您想更进一步,我建议您查看Doctrine Query Builder文档 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/query-builder.html
希望这有帮助
修改:WEEKS在Doctrine中不是原生的,但您可以将其用于https://github.com/beberlei/DoctrineExtensions