Laravel 查询满足条件的数据,并且获取的指定条数
有下面这张数据表。 目前想用一条sql查询 分别满足 is_hot = 1 、is_rec = 1 or is_end =1中每个条件的指定条数的数据
id | name | is_hot | is_rec | is_end | status |
---|---|---|---|---|---|
1 | 名称1 | 1 | 0 | 0 | 1 |
2 | 名称5 | 1 | 0 | 0 | 1 |
3 | 名称2 | 0 | 1 | 0 | 1 |
4 | 名称3 | 0 | 1 | 0 | 1 |
5 | 名称4 | 0 | 0 | 1 | 1 |
6 | 名称6 | 0 | 0 | 1 | 1 |
目前使用 or 做条件查询
$this->model
->orWhere(function($query) {
$query->where(‘is_hot’, 1)->take(5);
})
->orWhere(function($query) {
$query->where(‘is_rec’, 1)->take(3);
})
->orWhere(function($query) {
$query->where(‘is_end’, 1)->take(4);
})
->where(‘status’, 1)
->get();
有时候有随机性,有时候某个条件的查询不到,问过别人说要用then?
但是这个方法没有在手册里面找到,但是翻到了when
尝试着也没有写出来~
目前想要的结果:
- is_hot = 1 5条
- is_end =1 4条
- is_rec =1 3条
- 一共 12条数据
sql:
CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`is_end` smallint(1) DEFAULT NULL,
`is_hot` smallint(1) DEFAULT NULL,
`is_rec` smallint(1) DEFAULT NULL,
`status` smallint(1) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
模拟数据:
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称1', 1, 0, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称2', 1, 0, 0, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称3', 1, 0, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称4', 1, 0, 0, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称5', 1, 0, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称6', 0, 1, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称7', 0, 1, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称8', 0, 1, 0, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称9', 0, 1, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称10', 0, 1,0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称11', 0, 1, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称12', 0, 1, 0, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称13', 0, 1, 0, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称14',0, 1, 0, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称15', 0, 0, 1, 1);
INSERT INTO `book`.`article`( `name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称16', 0, 0, 1, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称17', 0, 0, 1, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称18', 0, 0, 1, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称19', 0 0, 1, 1);
INSERT INTO `book`.`article`(`name`, `is_end`, `is_hot`, `is_rec`, `status`) VALUES ('名称20', 0, 0, 1, 1);
可以使用 SQL里的 union all 方法,不过我更推荐将所有符合的结果查询出来之后,再利用Laravel 的 集合进行filter和merge 处理,这样性能会好一些