尝试多次选择时
$result = $this->qb->select('c.id','c.message','acc.name as chat_from', 'c.chat_to as count')
->addSelect("SELECT * FROM chat ORDER BY date_deliver DESC")
->from($this->table,'c')
->join('c','account','acc', 'c.chat_from = acc.id')
->orderBy('date_sent','DESC')
->groupby('chat_from')
->where('chat_to ='.$id)
->execute();
return $result->fetchAll();
我也试过
$result = $this->qb->select('c.id','c.message','acc.name as chat_from', 'c.chat_to as count')
->from("SELECT * FROM chat ORDER BY date_deliver DESC",'c')
->join('c','account','acc', 'c.chat_from = acc.id')
->orderBy('date_sent','DESC')
->groupby('chat_from')
->where('chat_to ='.$id)
->execute();
return $result->fetchAll();
我希望按组显示数据,然后显示最后一个条目中的数据。
我使用了DOCTRINE DBAL
请帮助答案 0 :(得分:1)
由于你的问题不清楚,所以我假设你需要获得每组最近的聊天/消息,相应的SQL将是
SELECT c.id, c.message, a.name as chat_from, c.chat_to as count
FROM account a
JOIN chat c ON(c.chat_from = a.id )
LEFT JOIN chat cc ON(c.chat_from = cc.chat_from AND c.date_sent < cc.date_sent)
WHERE cc.date_sent IS NULL AND c.chat_to = @id
ORDER BY c.date_sent DESC
因此,使用doctrine dbal,您可以将上述查询写为
$this->qb->select( 'c.id', 'c.message', 'a.name as chat_from', 'c.chat_to as count' )
->from( 'account', 'a' )
->join( 'a', 'chat', 'c', 'c.chat_from = a.id' )
->leftJoin( 'c', 'chat', 'cc', 'c.chat_from = cc.chat_from AND c.date_sent < cc.date_sent' )
->where( 'cc.date_sent IS NULL' )
->andWhere( 'c.chat_to =' . $id )
->orderBy( 'c.date_sent', 'DESC' )
->execute();
再次没有查看样本数据和DDL,它不是一个完整的解决方案。