我有一个基于学说的应用程序,我想从两个单独的表中选择行,并按创建日期对它们进行排序。我现在正(以某种方式)以这种方式完成它:
$query = $em->createQueryBuilder()
->from('Embed', 'e')
->select("e")
->leftJoin("User", "u", "WITH", "e.uid=u.id")
->leftJoin("Product", "p", "WITH", "e.pid=p.id")
->where("u.image > 0 OR p.image > 0")
->addOrderBy("u.timeCreated + p.timeCreated", "DESC")
->setMaxResults(28)
->getQuery();
我希望将产品和用户数据混合,按创建日期排序,但排序不正确。我也试过使用两个addOrderBy语句,它们正确地命令用户和产品,但是将所有用户放在结果中,而不是混合它们。
编辑:Rufo先生有正确的方法 - 这是工作代码:
$query = $em->createQueryBuilder()
->from('Embed', 'e')
->select("e")
->leftJoin("User", "u", "WITH", "e.uid=u.id")
->leftJoin("Product", "p", "WITH", "e.pid=p.id")
->addSelect('COALESCE( u.timeCreated, p.timeCreated ) as timeCreated')
->where("u.image > 0 OR p.image > 0")
->orderBy("timeCreated", "DESC")
->setMaxResults(28)
->getQuery();
答案 0 :(得分:3)
你应该试试这个
$query = $em->createQueryBuilder()
->from('Embed', 'e')
->select("e")
->leftJoin("User", "u", "WITH", "e.uid=u.id")
->leftJoin("Product", "p", "WITH", "e.pid=p.id")
->where("u.image > 0 OR p.image > 0")
->addOrderBy("COALESCE( u.timeCreated, p.timeCreated )", "DESC")
->setMaxResults(28)
->getQuery();