我的查询如下:
SELECT
*
FROM
(SELECT ks, COUNT(*) AS '# Tasks' FROM Table GROUP BY ks) t1
INNER JOIN
(SELECT ks, COUNT(*) AS '# Late' FROM Table WHERE Age > Palt GROUP BY ks) t2
ON t1.ks = t2.ks
我想将其更改为雄辩的laravel。我需要使用Laravel雄辩的分页方法
我这样尝试:
$query = \DB::select(\DB::raw("
SELECT *
FROM (
SELECT ks, COUNT(*) AS '# Tasks' FROM Table GROUP BY ks
) t1
INNER JOIN (
SELECT ks, COUNT(*) AS '# Late' FROM Table WHERE Age > Palt GROUP BY ks
) t2 ON t1.ks = t2.ks
"));
我很困惑使用paginate
方法。我需要它。因为我想在视图刀片Laravel中显示分页
这样的脚本:{{ $results->appends(Request::query())->render() }}
我该怎么办?
答案 0 :(得分:1)
首先,基于您作为数据库架构来源的linked question,可以使用更简单的查询找到要查询的数据。
我已经编写了以下查询,供在MySQL DBMS上使用,因为您的问题或链接的问题都未指定DBMS类型。
SELECT ks, COUNT(*) AS tasks, SUM(age < palt) AS late
FROM tasks
GROUP BY ks;
这是一个SQL Fiddle,其中包含示例数据,说明了查询的工作原理。
第二,可以使用Laravel查询构建器按如下方式构建上述查询。
$results = DB::table('tasks')
->selectRaw('ks, COUNT(*) AS tasks, SUM(age < palt) AS late')
->groupBy('ks')
->paginate(10);