Laravel,我如何简单地执行复杂的查询
Laravel, how I can simply execute complex query
我需要统计一个月内发送的电子邮件,最后发送给每个人。
我想象这样的事情:
$mes=8; // or any month
$search= [100,101,102,103]; // dni
$sql = "
SELECT A.dni, $mes AS mes, COUNT(*) AS total, B.lastet_date
FROM emails AS A
LEFT JOIN (
SELECT dni, MAX(date_sended) AS lastet_date
FROM emails
GROUP BY dni
) AS B
ON B.dni = A.dni
WHERE MONTH(date_sended) = $mes
AND A.dni IN ('".implode("', '",$search)."')
GROUP BY A.dni";
我怎样才能简单地使用原始方法或类似方法执行它?
您可以使用 DB::select
助手:
use Illuminate\Support\Facades\DB;
...
$results = DB::select($sql)
这将return一个集合,其中每个元素都是查询结果的一行。
我需要统计一个月内发送的电子邮件,最后发送给每个人。 我想象这样的事情:
$mes=8; // or any month
$search= [100,101,102,103]; // dni
$sql = "
SELECT A.dni, $mes AS mes, COUNT(*) AS total, B.lastet_date
FROM emails AS A
LEFT JOIN (
SELECT dni, MAX(date_sended) AS lastet_date
FROM emails
GROUP BY dni
) AS B
ON B.dni = A.dni
WHERE MONTH(date_sended) = $mes
AND A.dni IN ('".implode("', '",$search)."')
GROUP BY A.dni";
我怎样才能简单地使用原始方法或类似方法执行它?
您可以使用 DB::select
助手:
use Illuminate\Support\Facades\DB;
...
$results = DB::select($sql)
这将return一个集合,其中每个元素都是查询结果的一行。