Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中创建子查询-查询生成器_Php_Laravel_Laravel 5_Eloquent_Query Builder - Fatal编程技术网

Php 在Laravel中创建子查询-查询生成器

Php 在Laravel中创建子查询-查询生成器,php,laravel,laravel-5,eloquent,query-builder,Php,Laravel,Laravel 5,Eloquent,Query Builder,我尝试创建一个子查询。目前,我的两个问题是: select `key` from messages group by `key`; 及 目的是突出显示我的表消息所有按键分组的元素,并保留最后一个元素(在desc创建) 谢谢你像这样的方法应该会奏效: $data = DB::table('messages')->whereIn('key',function($q) { $q->select('key')->from('messages')->groupBy('ke

我尝试创建一个子查询。目前,我的两个问题是:

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

目的是突出显示我的表
消息
所有按键分组的元素,并保留最后一个元素(在desc创建)


谢谢你

像这样的方法应该会奏效:

$data = DB::table('messages')->whereIn('key',function($q) {
    $q->select('key')->from('messages')->groupBy('key');
})->latest()->take(1)->get();

你可以用两种不同的方法来做这件事

查询生成器:

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();
如果我们假设你们有这样的桌子

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)
对于两个不同的查询,结果如下

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)

它不起作用,因为它引用了那个录音。我给你拍了一张我所拥有的东西的快照。谢谢你,还有
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)
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)