等同于mysql / laravel中的限定语句?(Equivalent to a qualify statement in mysql/laravel?)
我有一个如下所示的数据库:
ID | facebook_id | likes | created_at
--------------------------------------------------
1 | x | 100 | 2017-01-01 12:10:15
2 | x | 110 | 2017-01-02 12:10:15
3 | y | 115 | 2017-01-01 12:10:15
4 | y | 120 | 2017-01-02 12:10:15
5 | z | 200 | 2017-01-01 12:10:15
6 | z | 201 | 2017-01-01 12:10:15
7 | z | 200 | 2017-02-02 12:10:15
8 | z | 205 | 2017-02-03 12:10:15
9 | z | 250 | 2017-03-04 12:10:15
我想获取表格中每个独特facebook_id的最新记录,然后按likes的descending排列。 我为数据库使用laravel 5.4和mysql 。 在teradata,我会做类似的事情
QUALIFY ROW_NUMBER() OVER(PARTITION BY facebook_id ORDER BY created_at DESC) = 1
通过facebook_id订购结果并通过独特的facebook_id获取最新的行。 但我不确定如何在laravel / mysql中执行此操作。
我努力了
Database::select('facebook_id', 'likes', 'created_at')
->orderBy('likes', 'desc')
->distinct('facebook_id')
->latest('created_at')
->limit(20)
->get();
但它只是由likes订购,并没有让我独特的facebook_id的
我的结果是:
Top Profile ID: z Likes:250
Top Profile ID: z Likes:205
Top Profile ID: z Likes:201
Top Profile ID: z Likes:200
我想得到的是:
Top Profile ID: z Likes:250
Top Profile ID: y Likes:120
Top Profile ID: x Likes:110
I have a database that looks like this:
ID | facebook_id | likes | created_at
--------------------------------------------------
1 | x | 100 | 2017-01-01 12:10:15
2 | x | 110 | 2017-01-02 12:10:15
3 | y | 115 | 2017-01-01 12:10:15
4 | y | 120 | 2017-01-02 12:10:15
5 | z | 200 | 2017-01-01 12:10:15
6 | z | 201 | 2017-01-01 12:10:15
7 | z | 200 | 2017-02-02 12:10:15
8 | z | 205 | 2017-02-03 12:10:15
9 | z | 250 | 2017-03-04 12:10:15
I want to get the latest record for each of the unique facebook_id's in the table then order it by likes in descending order. I am using laravel 5.4 and mysql for the database. In teradata I would do something like
QUALIFY ROW_NUMBER() OVER(PARTITION BY facebook_id ORDER BY created_at DESC) = 1
to order the results by facebook_id and get the latest row by unique facebook_id. But I'm not sure how to do this in laravel/mysql.
I have tried
Database::select('facebook_id', 'likes', 'created_at')
->orderBy('likes', 'desc')
->distinct('facebook_id')
->latest('created_at')
->limit(20)
->get();
But it just orders it by likes and doesnt get me unique facebook_id's
My results are:
Top Profile ID: z Likes:250
Top Profile ID: z Likes:205
Top Profile ID: z Likes:201
Top Profile ID: z Likes:200
What I want to get is:
Top Profile ID: z Likes:250
Top Profile ID: y Likes:120
Top Profile ID: x Likes:110
原文:https://stackoverflow.com/questions/42596082
更新时间:2019-12-30 22:46
最满意答案
这个问题:
我想获取表格中每个独特facebook_id的最新记录,然后按喜欢的降序排列。
有一个答案。 有多种方法。 这里是一个:
select t.*
from t
where t.created_at = (select max(t2.create_at)
from t t2
where t2.facebook_id = t.facebook_id
)
order by t.likes desc;
This question:
I want to get the latest record for each of the unique facebook_id's in the table then order it by likes in descending order.
has an answer. There are multiple methods. Here is one:
select t.*
from t
where t.created_at = (select max(t2.create_at)
from t t2
where t2.facebook_id = t.facebook_id
)
order by t.likes desc;
2017-03-04
相关问答
SQLite命令行实用程序具有.schema TABLENAME命令,显示create语句。 The SQLite command line utility has a .schema TABLENAME command that shows you the create statements.
您可以创建相应的C enum ,并将其作为整数类型存储在内存中。 这比将它存储在NSString更快/更小,但除非你做了一些非常不寻常的事情(即处理数百万条记录),否则我不希望这些区别是显而易见的。 如果你确实选择为此目的制作一个C enum ,你可能最终会编写例程来往返于NSString s来回转换它,这是一个次要但令人讨厌的苦差事。 这些天在Objective-C中用于这种enums的惯用事物是NS_ENUM宏。 有关详细说明,请参阅此页面 。 You can make a correspon
...
select object_id
from tags_link
where tag_id in (31,35)
group by object_id
having count(distinct tag_id) = 2
select object_id
from tags_link
where tag_id in (31,35)
group by object_id
having count(distinct tag_id) = 2
这个问题: 我想获取表格中每个独特facebook_id的最新记录,然后按喜欢的降序排列。 有一个答案。 有多种方法。 这里是一个: select t.*
from t
where t.created_at = (select max(t2.create_at)
from t t2
where t2.facebook_id = t.facebook_id
)
order
...
在SQL Server 2012中,支持ANSI标准的OFFSET / FETCH语法。 我在这里发表了博客 ,这里是官方文档 (这是对ORDER BY的扩展)。 您为SQL Server 2012转换的语法是: SELECT ID, Name, Price, Image
FROM Products
ORDER BY ID ASC
OFFSET (@start_from - 1) ROWS -- not sure if you need -1
-- because I d
...
PHP 5.5.12,Windows 我做了更多的测试。 我创建了一个包含100 000条记录的表格,并且我检查了当表格中的所有值用不同的方法求和时会占用多少内存: +----------+-------------+--------+
| method | memory used | time |
+----------+-------------+--------+
| fetchAll | 69.9157 MB | 7.20 s | //
...
据我所知,在MonetDB中没有与此相同的功能 。 if()函数是MySQL Extensions to Standard SQL的一部分 ,但这个扩展不是monet内置函数的一部分 像许多其他SQL数据库一样, monetdb中也没有'IF()'函数,因为它不是标准SQL语言的一部分。 对于其他证据,请参阅此问题并在标准解析器中尝试查询。 我知道你不是在寻找一个案例陈述 ,无论如何,根据SQL中的一个Nutshell : ANSI SQL2003提供了一个名为CASE的函数 ,可用于在查询或更新
...
试试这个 CREATE TABLE countries
(
COUNTRY_ID varchar(2) PRIMARY KEY,
COUNTRY_NAME varchar(40) DEFAULT NULL,
REGION_ID decimal(10,0) DEFAULT NULL
)
Try this CREATE TABLE countries
(
COUNTRY_ID varchar(2) PRIMARY KEY,
...