dongnao6858 2015-10-31 09:18
浏览 305
已采纳

在Laravel中创建子查询 - 查询构建器

I try to create a sub query. For now, my two queries are :

select `key` from messages group by `key`;

and

select * from messages where `key` = 'KEY_RECUP_AU_DESSUS' order by created_at DESC LIMIT 1;

The aim is to highlight my table messages all elements grouped by key and keeping the last element ( created_at desc)

Thank you

  • 写回答

2条回答 默认 最新

  • dsuvs66406 2015-10-31 19:04
    关注

    You can do this with two different ways.

    Query Builder:

    DB::table('messages AS a')
    ->leftJoin('messages AS b', function($join) {
        $join->on('a.created_at', '<', 'b.created_at');
        $join->on('a.key', '=', 'b.key');
    })
    ->groupBy(['a.key', 'a.message', 'a.created_at'])
    ->havingRaw('COUNT(b.key) < 1')
    ->select(['a.key', 'a.message', 'a.created_at'])
    ->get();
    

    Another way with PARTITION BY

    SELECT 
          key,
          message,
          created_at
    FROM (
        SELECT 
              key, 
              message, 
              created_at, 
              rank() OVER (PARTITION BY key ORDER BY created_at DESC) AS rank 
        FROM 
              messages
    ) AS foo WHERE foo.rank = 1;
    

    if we assume you have table like this.

    postgres=# select * from messages;
     id |   key   |        message         |         created_at
    ----+---------+------------------------+----------------------------
      1 | contact | send from contact page | 2015-10-31 19:45:16.850698
      2 | contact | contact page message   | 2015-10-31 19:45:34.417231
      3 | product | product 1              | 2015-10-31 19:45:44.49584
      4 | product | product 2              | 2015-10-31 19:45:46.856691
      5 | contact | hello it is me         | 2015-10-31 18:45:35.801967
      6 | about   | who are you            | 2015-10-31 19:46:04.123369
      7 | product | product 3              | 2015-10-31 19:46:12.414364
      8 | about   | hi guys                | 2015-10-31 19:46:18.23442
    (8 rows)
    

    The result will be like this with two different query

    postgres=# select key, message, created_at from (select key, message, created_at, rank() over (partition by key order by created_at desc) as rank from messages) foo where rank = 1;
       key   |       message        |         created_at         
    ---------+----------------------+----------------------------
     about   | hi guys              | 2015-10-31 19:46:18.23442  
     contact | contact page message | 2015-10-31 19:45:34.417231 
     product | product 3            | 2015-10-31 19:46:12.414364 
    (3 rows)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM