如何将代码sql
转换为eloquent
?
SELECT
CONCAT(FLOOR(sum(diferenca)/60),'h',MOD(sum(diferenca),60),'m') as tempo
FROM
(SELECT
TIMESTAMPDIFF(MINUTE, m1.created_at, min(m2.created_at)) as diferenca
FROM
mensagens m1
JOIN
mensagens m2 ON m1.remetente_id = m2.destinatario_id AND m2.remetente_id = m1.destinatario_id AND m2.created_at > m1.created_at
GROUP BY
m1.id) AS table1
WHERE
(SELECT
MIN(id)
FROM
mensagens
WHERE
destinatario_id = 2);
我尝试这样,但没有成功。给我错误,因为它将concat select视为一列。
Mensagem::whereIn('id', function($query) {
$query->selectRaw('TIMESTAMPDIFF(MINUTE, m1.created_at, min(m2.created_at)) as diferenca')
->from('mensagens as m1')
->join('mensagens as m2', 'm1.remetente_id', '=', 'm2.destinatario_id')
->where('m2.created_at', '>', 'm1.created_at')
->groupBy('m1.id');
})
->where(function($query) {
$query->selectRaw('min(id)')
->from('mensagens')
->where('destinatario_id', 2);
})
->select("CONCAT(FLOOR(sum(diferenca)/60),'h',MOD(sum(diferenca),60),'m') as tempo")
->get();