php - PHP 和 Laravel | 在 GroupBy 之后使用 OrderBy
问题描述
这是我的 PHP 代码
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->get();
我想应用 orderby 和 limit('5')count
有什么方向吗?谢谢你。
解决方案
添加 orderBy() ..和 take()
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->orderBy('your_column')
->take(5)
->get();
推荐阅读
- python-3.x - odoo 12中的Keyerror'job.cost.sheet'
- sql - SQL - 仅为日期最短的操作添加 +1
- pandas - 训练模型时出现张量流错误-标签dtype应该是整数而不是得到
- react-native - 将 API KEY 放入 axios
- angular - Firefox 导致 POST 请求被调用两次
- sql-server - AWS RDS SQL Server:将大数据从存储过程导出到 CSV 文件
- node.js - dialogflow nodejs函数返回空对象而不是字符串
- tensorflow - Tensorflow GPU 自定义对象检测不起作用
- html - 省略号在搜索文本下方时突出显示
- elasticsearch - 当我名称错误时,Elasticsearch 找不到产品