[英]Returning Array in mysql Select
I'm trying to group inventory results by the model and manufacturer name, display the amount of items matching and 1 result per grouping. 我正在尝试按模型和制造商名称对库存结果进行分组,显示匹配的项目数量和每个分组的1个结果。 With that said, I'd like to try and retrieve all inventory id's within the group.
话虽如此,我想尝试检索组内的所有库存ID。 Wondering if this is possible... Any ideas?
想知道这是否可能......有什么想法吗?
FYI - I'm using Laravel, the line in question is the ->selectRaw(CambridgeID as CambridgeIDArray) 仅供参考 - 我正在使用Laravel,有问题的行是 - > selectRaw(CambridgeID为CambridgeIDArray)
$getMatchingInventory = DB::table('inventory')
->selectRaw('*, count(*) as groupTotal')
->whereRaw("MATCH(ManufacturerNameMatch, SubCategoryNameMatch, MainCategoryNameMatch, Model_Name, Title_Override, Description_Old) AGAINST ('$final' IN BOOLEAN MODE)")
->selectRaw('CambridgeID as CambridgeIDArray')
->groupBy('Model_Name', 'ManufacturerNameMatch')
->having('Units_OnHand', '>=', '1')
->orderBy('ManufacturerNameMatch')
//->paginate(15);
->get();
try this 尝试这个
$getMatchingInventory = DB::table('inventory')
->select(DB::raw("GROUP_CONCAT(CambridgeID) as `CambridgeIDArray`, count(*) as `groupTotal`"))
->whereRaw("MATCH(ManufacturerNameMatch, SubCategoryNameMatch, MainCategoryNameMatch, Model_Name, Title_Override, Description_Old) AGAINST ('$final' IN BOOLEAN MODE)")
->groupBy('Model_Name', 'ManufacturerNameMatch')
->having('Units_OnHand', '>=', '1')
->orderBy('ManufacturerNameMatch')
->get();
You should be able to use GROUP_CONCAT
for that, see: http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat 您应该能够使用
GROUP_CONCAT
,请参阅: http : //dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
You can use specify output format options (eg, SEPARATOR) and use additional string manipulation as needed within the GROUP_CONCAT
. 您可以使用指定输出格式选项(例如,SEPARATOR)并在
GROUP_CONCAT
根据需要使用其他字符串操作。
(Fyi, using raw MySQL, at least for this question, would make it easier to parse.) (Fyi,使用原始MySQL,至少对于这个问题,会更容易解析。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.