这样的MySQL查询:
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'ifnull(SUM(case when location_code = ''',
location_code ,
''' then quantity end),0) AS `',
location_code , '`'
)
) INTO @sql
FROM
item_details;
SET @sql = CONCAT('SELECT item_number,SUM(quantity) as "total_quantity", ', @sql, '
FROM item_details
GROUP BY item_number');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
我想雄辩地将其转换为laravel,但是我很困惑。因为存在很多陈述。存在PREPARE
,EXECUTE
,SET
,DEALLOCATE
等。您可以在上方看到查询
如何将其转换为雄辩的laravel?
答案 0 :(得分:3)
主要是原始查询:
DB::table('item_details')->selectRaw('GROUP_CONCAT(...) INTO @sql')->get();
DB::statement('SET @sql = CONCAT(...)');
DB::statement('PREPARE stmt FROM @sql');
DB::statement('EXECUTE stmt');
DB::statement('DEALLOCATE PREPARE stmt');
尝试一下:
DB::table('item_details')->selectRaw('GROUP_CONCAT(...) INTO @sql')->get();
$sql = DB::selectOne('select @sql')->{'@sql'};
ItemDetails::select('item_number', DB::raw('SUM(quantity) as total_quantity'))
->selectRaw($sql)
->groupBy('item_number')
->get();
答案 1 :(得分:2)
您应该将查询作为Mysql Procedure并针对您的数据库运行它,我在下面对您的查询做了一些调整,
DROP PROCEDURE IF EXISTS searchitems;
DELIMITER $$
CREATE PROCEDURE searchitems()
BEGIN
SET @@group_concat_max_len = 75000;
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(CASE WHEN location_code = ''',
location_code,
''' THEN coalesce(quantity, 0) END) AS `',
location_code, '`'
)
) INTO @sql
FROM
item_details;
SET @query := CONCAT('SELECT item_number,SUM(quantity) as "total_quantity", ', @sql, '
FROM item_details
GROUP BY item_number');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET @@group_concat_max_len = 1024;
END $$
DELIMITER ;
然后使用“查询生成器方法”从Laravel控制器调用过程,
$queryResult = $db->prepare('call searchitems()');
$queryResult->execute();
$results = $queryResult->fetchAll(PDOConnection::FETCH_ASSOC);
$queryResult->closeCursor();
然后将结果集转换为Laravel集合,然后您可以像这样轻松进行分页,
$results_collection = collect($results);
$currentPage = LengthAwarePaginator::resolveCurrentPage();
$perPage = 20;
$currentPageSearchResults = $results_collection->slice(($currentPage - 1) * $perPage, $perPage)->all();
$paginatedSearchResults = new LengthAwarePaginator($currentPageSearchResults, count($results_collection), $perPage);
$paginatedSearchResults->setPath($request->url());
$paginatedSearchResults->appends($request->except(['page']));
然后返回到这样的视图
return view('yourview')
->with('results',$paginatedSearchResults);
//or if ajax call
$viewData = view('yourview')
->with('results',$paginatedSearchResults)
->render();
$response = [
"Status" => "Success",
"Content" => $viewData
];
return response()->json($response);
,并在刀片视图模板中,您可以在foreach循环中访问数据并在表格或列表视图中进行渲染, 显示分页,您可以这样
//to render pagination is front end
<div class="row">
<div class="col-md-offset-4">
<?php echo $results->render(); ?>
</div>
</div>
但是我建议您缓存数据,以避免重复调用过程并提高性能,因为如果您有数百万个数据,这种构建分页的方式会降低应用程序的速度, 如果您需要实现Ajax分页, 您可以参考this article
答案 2 :(得分:0)
这似乎是重复的(How to create select dynamic fields from a table in mysql?)。
您在另一个线程上寻求并获得帮助,但是在这里询问如何实际实现他们在此给出的答案。 (我可能会补充说,在询问方面相当好斗。)
通常最好要求对所得到的代码进行澄清,以作为给出问题的答案。