select * from requirement_order ORDER BY (select goods_sku.price from goods_sku where goods_sku.requirement_id = requirement_order.id limit 1) desc
一对多限制从表条数查询,比如查出所有分类以及每个分类下n条文章
表结构如下
方法一
laravel构造器写法
$category = Category::select('id', 'name')
->with([
'article'=>function ($query) {
$query->select('id', 'cat_id', 'title', 'author')
->whereRaw('(select count(*) from articles as articles2 where articles2.cat_id = articles.cat_id and articles2.id >= articles.id) <= ?', [3]);
}
])
->get();
代码执行SQL
//查询分类
select `id`,`name` from `categories`
//查询分类下文章,每个分类下3条
select
`id`,
`cat_id`,
`title`,
`author`
from
`articles`
where
`articles`.`cat_id` in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
and (
select
count(*)
from
articles as articles2
where
articles2.cat_id = articles.cat_id
and articles2.id >= articles.id
) <= 3