php - 如何在 Laravel 中使用别名运行子查询?
问题描述
我有以下查询,我正在努力在 Laravel 中使用。DB::raw()
在那里但不知道如何设置子查询。
SELECT * from products
where status = 1
AND
id IN (
SELECT * FROM (
SELECT `product_id`
FROM `order_product`
order by `id` desc limit 8
) as T
)
我正在使用 Laravel 5.6
解决方案
尚未对其进行测试,但这样的事情应该可以解决问题:
$products = Product
::where('status', 1)
->whereIn('id', function ($query) {
$query
->select('product_id')
->from('order_product')
->orderBy('id', 'desc')
->take(8);
})
->get();
推荐阅读
- wordpress - Wordpress 电子邮件将成为垃圾邮件
- python - Pandas,使用条件语句过滤
- javascript - React 不加载本地图像
- powerbi - 如何过滤到特定类别,并屏蔽/匿名其余类别?
- java - 为什么说:“检查的异常不在调用链中转发(传播)”?
- string - 为什么是格式!宏比直接推入字符串慢?
- animation - 断断续续的 svg 动画
- django - Django Rest 多张图片上传到 React-Native
- uwp - HoloLens 无法在开发者模式下与我的电脑配对
- ruby-on-rails - 为什么我不能在规范中创建用户?