Php 显示计数和所有血液成分
我试图显示同名表项的计数 我有一张这样的桌子Php 显示计数和所有血液成分,php,laravel,Php,Laravel,我试图显示同名表项的计数 我有一张这样的桌子 Blood Collection -------------------- id bloodComponent 1 Whole Blood 2 Platelet 3 Plasma 4 Platelet 5 Plasma 6 Whole Blood 我希望输出类似于: Name On Stock ------------------------ Whole Blood 2 Plate
Blood Collection
--------------------
id bloodComponent
1 Whole Blood
2 Platelet
3 Plasma
4 Platelet
5 Plasma
6 Whole Blood
我希望输出类似于:
Name On Stock
------------------------
Whole Blood 2
Platelet 2
Plasma 2
我正在使用laravel作为框架…您可以尝试以下方法:
$bloodCollections = DB::table('blood_collections')
->select('bloodComponent as name', DB::raw('count(*) as onStock'))
->groupBy('bloodComponent')
// Prepare $startDate & $endDate
->whereBetween('created_at', [$startDate, $endDate])
->get();
然后循环:
foreach($bloodCollections as $collection) {
// $collection['name'];
// $collection['onStock'];
}
您可以尝试以下方法:
$bloodCollections = DB::table('blood_collections')
->select('bloodComponent as name', DB::raw('count(*) as onStock'))
->groupBy('bloodComponent')
// Prepare $startDate & $endDate
->whereBetween('created_at', [$startDate, $endDate])
->get();
然后循环:
foreach($bloodCollections as $collection) {
// $collection['name'];
// $collection['onStock'];
}
在您的结果中,您将在bloodComponent
key中有名称,在tot
key中有单个总计数
在您的结果中,您将在
bloodComponent
key中有名称,在tot
keysql101中有单个总计数<代码>选择'bloodComponent'作为'name',按'bloodComponent'从'bloodTable'组中将(1)作为'On Stock'SQL 101..-<代码>选择'bloodComponent'作为'name',从'bloodTable'组中按'bloodComponent'将(1)计数为'On Stock'这是可行的,但如何输出计数?它不会显示在您的查询中DB::raw('count(*)作为onStock
这将为您提供onStock
作为字段。谢谢您,先生,这工作很好…我只想添加一些…这是我的库存报告…我想显示一份从2016-02-01到2016-02-21的血液报告。我该怎么做?先生,您从哪里得到创建的?一切都很好…非常感谢您,先生。我非常感谢您ciate它可以工作,但如何输出计数?它不会显示在您的查询中DB::raw('count(*)作为onStock
这将为您提供onStock
作为字段。谢谢您,先生,这工作很好…我只想添加一些…这是我的库存报告…我想显示一份从2016-02-01到2016-02-21的血液报告。我该怎么做?先生,您从哪里得到创建的?一切都很好…非常感谢您,先生。我非常感谢您联系它