laravel - 如何查询与最大数量的三个关系?
问题描述
我有 3 个模型
1.SellInvoice模型:
protected $table = "sell_invoices";
public function jewelsItems(){
return $this->hasMany(JewelsItem::class,'buy_invoice_id');
}
2.宝石型号:
protected $table = 'jewel';
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
3.JewelsItem型号:
protected $table = 'jewel_items';
public function jewel(){
return $this->belongsTo('App\Models\Jewel');
}
public function sellInvoice(){
return $this->belongsTo(SellInvoice::class,'sell_invoice_id');
}
查询:在我的数据库中查找 10 个最畅销的珠宝或对某个日期的销售数量进行排序。
注意:我在 SellInvoice 中有一个 InvoiceDate,必须使用它来了解从某个日期开始最畅销的珠宝
尝试:我尝试通过销售的商品获取组,该商品的销售日期为 2021-03-1:
JewelsItem::whereHas('sellInvoice',function ( $query) {
$query->where('InvoiceDate','>=','2021-03-1');
})->groupby('jewel_id')->get();
但这让我出错:
语法错误或访问冲突:1055 'laravel_goldshop.jewel_items.id' 不在 GROUP BY 中(SQL:select * from
jewel_items
where exists (select * fromsell_invoices
wherejewel_items
.sell_invoice_id
=sell_invoices
.id
andInvoiceDate
>= 2021-03-1) group byjewel_id
)
解决方案
set global
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
推荐阅读
- python-3.x - 如何通过寻路游戏避免即时“游戏结束”?
- java - 如何将鼠标侦听器添加到多个 JLabel []
- vba - 将输入值从输入框传递到另一个私有子代码?
- android - 将标高设置为选项卡布局的顶部
- python - Python:将参数传递给文件(python file1 -c file2 argfile2)
- javascript - 如何从重复的文本模式中提取内容?
- google-cloud-storage - 下载前获取图像大小
- angular - 在 Angular 路由中,当组件是共享组件时,我如何配置路由?
- node.js - Hyperledger Fabric peer 实例化链码错误
- c - 在过滤器驱动程序中删除文件时生成的 IRP 消息是什么?