Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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
MySQL:按最新日期时间列排序?_Mysql_Sql_Subquery_Sql Order By_Inner Join - Fatal编程技术网

MySQL:按最新日期时间列排序?

MySQL:按最新日期时间列排序?,mysql,sql,subquery,sql-order-by,inner-join,Mysql,Sql,Subquery,Sql Order By,Inner Join,以下是我添加订单之前的查询: SELECT `picture` FROM `profile_data` WHERE `profile_id` IN ( SELECT `profile_id` FROM `collection_entries` WHERE `collection_id` = 2 ) LIMIT 10; 我尝试添加如下顺序: SELECT `picture` FROM `profile_data` WHERE `profile_id` IN ( SELE

以下是我添加订单之前的查询:

SELECT `picture` 
FROM `profile_data` 
WHERE `profile_id` IN (
    SELECT `profile_id` FROM `collection_entries` WHERE `collection_id` = 2
) 
LIMIT 10;
我尝试添加如下顺序:

SELECT `picture` 
FROM `profile_data` 
WHERE `profile_id` IN (
    SELECT `profile_id` 
    FROM `collection_entries` 
    WHERE `collection_id` = 2 
    ORDER BY `created_at` DESC
) 
LIMIT 10;
虽然我收到了这个问题:

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

有什么方法可以做我想做的事情吗?

您可以加入,这样在外部查询中就可以使用
created\u at
列进行排序:

SELECT `picture` 
FROM `profile_data` pd
INNER JOIN `collection_entries` ce ON ce.`profile_id` = pd.`profile_id`
WHERE ce.`collection_id` = 2
ORDER BY ce.`created_at` DESC
LIMIT 10;
这假设每个
profile\u数据在
collection\u条目中不超过一行。如果不是这样,那么您需要先进行预聚合。因此:

SELECT `picture` 
FROM `profile_data` pd
INNER JOIN (
    SELECT `profile_id`, MAX(`created_at`) `created_at`
    FROM `collection_entries` 
    WHERE `collection_id` = 2
    GROUP BY `profile_id`
) ce ON ce.`profile_id` = pd.`profile_id`
ORDER BY ce.`created_at` DESC
LIMIT 10;

您可以改为加入,以便在外部查询中可以使用在
处创建的
列进行排序:

SELECT `picture` 
FROM `profile_data` pd
INNER JOIN `collection_entries` ce ON ce.`profile_id` = pd.`profile_id`
WHERE ce.`collection_id` = 2
ORDER BY ce.`created_at` DESC
LIMIT 10;
这假设每个
profile\u数据在
collection\u条目中不超过一行。如果不是这样,那么您需要先进行预聚合。因此:

SELECT `picture` 
FROM `profile_data` pd
INNER JOIN (
    SELECT `profile_id`, MAX(`created_at`) `created_at`
    FROM `collection_entries` 
    WHERE `collection_id` = 2
    GROUP BY `profile_id`
) ce ON ce.`profile_id` = pd.`profile_id`
ORDER BY ce.`created_at` DESC
LIMIT 10;

in()
子句中排序没有用。在
in()
子句中排序没有用。我以为Gordon是这个网站上唯一的SQL机器。我错了+1。@TimBiegeleisen:我确实认为这是一种恭维。。。谢谢我以为Gordon是这个网站上唯一的SQL机器。我错了+1。@TimBiegeleisen:我确实认为这是一种恭维。。。谢谢