简体   繁体   English

Laravel 5-检索数据库中每个类别的前4条记录

[英]Laravel 5 - Retrieve the first 4 records for each category in database

I have a database table of gallery images which are categorised by the following: 我有一个图库图像的数据库表,这些图像按以下类别分类:

'corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue' “公司”,“食品”,“公园”,“聚会”,“骑行”,“学校”,“场地”

Each image has one of these categories assigned to it. 每个图像都分配有以下类别之一。

I'm building a main gallery page in which I want to display the latest 4 images from each of these categories in the database. 我正在建立一个主画廊页面,在其中我想显示数据库中每个类别的最新4张图像。

Could someone assist as to how I can go about building the query? 有人可以协助我如何建立查询吗?

The query starts as follows: 查询开始如下:

Bugz\GalleryImage::

Table Structure 表结构

Schema::create('gallery_images', function (Blueprint $table) {

            //set the table engine:
            $table->engine = 'InnoDb';

            //define an auto-incrementing primary key:
            $table->increments('id');

            //define the general fields:
            $table->enum('gallery', array('corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue'))->default('corporate');
            $table->string('title');
            $table->string('content')->nullable()->default(null);

            //define the audit fields:
            $table->timestamps();
            $table->softDeletes();

        });

I don't have enough experience yet with Eloquent to write more complex queries. 我没有Eloquent足够的经验来编写更复杂的查询。

Thank you. 谢谢。

Here's one way... 这是一种方法

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(image_id INT NOT NULL ATO_INCREMENT PRIMARY KEY
, category ENUM('corporate', 'food', 'park', 'parties', 'rides', 'schools', 'venue') NOT NULL
);

INSERT INTO my_table (category) VALUES
('corporate'), 
('food'), 
('park'), 
('parties'), 
('rides'), 
('schools'), 
('venue'),
('rides'), 
('schools'), 
('venue'),
('food'), 
('park'), 
('parties'), 
('rides'), 
('corporate'), 
('food'), 
('park'), 
('food'), 
('park'), 
('parties'), 
('rides'), 
('food'), 
('park'), 
('food'), 
('corporate'), 
('rides'), 
('corporate'), 
('parties'), 
('rides'), 
('corporate'), 
('food'),
('schools'), 
('venue'),
('venue'),
('food'), 
('park'), 
('parties')
;

Intermediate result... 中间结果...

SELECT x.*
     , COUNT(y.image_id) temp_ranks_for_y
  FROM my_table x 
  JOIN my_table y   
    ON y.category = x.category 
   AND y.image_id >= x.image_id 
 GROUP 
    BY x.image_id;
+----------+-----------+-------------------+
| image_id | category  | temp_ranks_for_y  |
+----------+-----------+-------------------+
|        1 | corporate |                 5 |
|        2 | food      |                 8 |
|        3 | park      |                 6 |
|        4 | parties   |                 5 |
|        5 | rides     |                 6 |
|        6 | schools   |                 3 |
|        7 | venue     |                 4 |
|        8 | rides     |                 5 |
|        9 | schools   |                 2 |
|       10 | venue     |                 3 |
|       11 | food      |                 7 |
|       12 | park      |                 5 |
|       13 | parties   |                 4 |
|       14 | rides     |                 4 |
|       15 | corporate |                 4 |
|       16 | food      |                 6 |
|       17 | park      |                 4 |
|       18 | food      |                 5 |
|       19 | park      |                 3 |
|       20 | parties   |                 3 |
|       21 | rides     |                 3 |
|       22 | food      |                 4 |
|       23 | park      |                 2 |
|       24 | food      |                 3 |
|       25 | corporate |                 3 |
|       26 | rides     |                 2 |
|       27 | corporate |                 2 |
|       28 | parties   |                 2 |
|       29 | rides     |                 1 |
|       30 | corporate |                 1 |
|       31 | food      |                 2 |
|       32 | schools   |                 1 |
|       33 | venue     |                 2 |
|       34 | venue     |                 1 |
|       35 | food      |                 1 |
|       36 | park      |                 1 |
|       37 | parties   |                 1 |
+----------+-----------+-------------------+

So... 所以...

SELECT x.* 
  FROM my_table x 
  JOIN my_table y 
    ON y.category = x.category 
   AND y.image_id >= x.image_id 
 GROUP 
    BY x.image_id 
HAVING COUNT(y.image_id) <=4 
 ORDER 
    BY category 
     , image_id DESC;
+----------+-----------+
| image_id | category  |
+----------+-----------+
|       30 | corporate |
|       27 | corporate |
|       25 | corporate |
|       15 | corporate |
|       35 | food      |
|       31 | food      |
|       24 | food      |
|       22 | food      |
|       36 | park      |
|       23 | park      |
|       19 | park      |
|       17 | park      |
|       37 | parties   |
|       28 | parties   |
|       20 | parties   |
|       13 | parties   |
|       29 | rides     |
|       26 | rides     |
|       21 | rides     |
|       14 | rides     |
|       32 | schools   |
|        9 | schools   |
|        6 | schools   |
|       34 | venue     |
|       33 | venue     |
|       10 | venue     |
|        7 | venue     |
+----------+-----------+
27 rows in set (0.00 sec)

This code will retrives 4 images of every category. 此代码将检索每个类别的4张图像。 if you want to retrive latest one then just add sort by cluse in outer query. 如果您要检索最新的,则只需在外部查询中添加按类排序。

    DB::table('gallery_images AS GI')
    ->where(function($query)
        {
            DB::table('tbl_dept_master AS GI2')
            ->select(DB::raw(count(1)))
            ->from('gallery_images As GI2')
            ->where('GI2.gallery','=','GI.gallery')
            ->where('GI2.title','>=','GI.title');
        })
    ->get();

If need more help comment. 如果需要更多帮助评论。 Hope this will sovle your problem. 希望这能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM