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);
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
Epona
最佳答案

可以使用 SQL里的 union all 方法,不过我更推荐将所有符合的结果查询出来之后,再利用Laravel 的 集合进行filter和merge 处理,这样性能会好一些

4年前 评论
讨论数量: 9
Epona

可以使用 SQL里的 union all 方法,不过我更推荐将所有符合的结果查询出来之后,再利用Laravel 的 集合进行filter和merge 处理,这样性能会好一些

4年前 评论

@Epona 是有考虑性能方面想高一点, 主要是也有点担心数据量大的话。一次性查出来性能也不好。而且这几个条件没有做索引。

4年前 评论
Epona

@Token 在SQL上执行多条语句查询比在 PHP里面处理要慢不少。

4年前 评论
宇宙最厉害

是不是想要这样的效果?

$this->model
->when(request('is_hot', false), function ($query, $value) {
    $query->orWhere('is_hot', $value)->take(5);
})
->when(request('is_rec', false), function ($query, $value) {
    $query->orWhere('is_rec', $value)->take(3);
})
->when(request('is_end', false), function ($query, $value) {
    $query->orWhere('is_end', $value)->take(4);
})
->where(‘status’, 1)
->get();

when 条件语句:

查询构造器《Laravel 5.8 中文文档》

4年前 评论

@沈益飞 谢谢回答~。 另外这个方法我也试验了以下。 如果都是非false 的时候。会查询第一个条件的条数。。 我现在想要如果同时三个条件都成立。然后分别查出来三个条件的take数的数据。 :blush: 感觉纯用mysql 貌似做不到了。。

4年前 评论
66
SELECT count(*) as count,'end_count' as `name` FROM `article` where is_end = 1
UNION
SELECT count(*) as count,'hot_count' as `name` FROM `article` where is_hot = 1

union 完美解决

4年前 评论

@66 博主不是这个意思吧

4年前 评论
66

@lovecn 额~~~那是啥。。

4年前 评论

这或许是你想要的答案,原生SQL :

SELECT sum(IF(is_end = 1, 1, 0)) as is_end_total, sum(IF(is_hot = 1, 1, 0)) as is_hot_total, sum(IF(is_rec = 1, 1, 0)) as is_rec_total from article where `status` = 1;
4年前 评论
hs1234 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!