Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.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 SQL选择唯一的用户列表,以及其他数据,具体取决于时间戳_Mysql_Sql_Group By_Distinct - Fatal编程技术网

Mysql SQL选择唯一的用户列表,以及其他数据,具体取决于时间戳

Mysql SQL选择唯一的用户列表,以及其他数据,具体取决于时间戳,mysql,sql,group-by,distinct,Mysql,Sql,Group By,Distinct,我在SQL中有一个表,该表如下所示: +-------------+---------------+-------------------------+ | quotes_user | email_address | ________Sent_at________ | +-------------+---------------+-------------------------+ | user1 | email1 | 2012-10-09 12:23:53.253 |

我在SQL中有一个表,该表如下所示:

+-------------+---------------+-------------------------+
| quotes_user | email_address | ________Sent_at________ |
+-------------+---------------+-------------------------+
| user1       | email1        | 2012-10-09 12:23:53.253 |
| user1       | email2        | 2012-10-09 12:24:53.253 |
| user2       | email3        | 2012-10-09 13:20:53.253 |
| user2       | email4        | 2012-10-09 11:23:53.253 |
| user3       | email5        | 2012-10-08 10:29:53.253 |
| user3       | email6        | 2012-10-08 14:23:53.253 |
+-------------+---------------+-------------------------+
我想让结果显示出来

+-------------+---------------+-------------------------+
| quotes_user | email_address | ________Sent_at________ |
+-------------+---------------+-------------------------+
| user1       | email2        | 2012-10-09 12:24:53.253 |
| user2       | email3        | 2012-10-09 13:20:53.253 |
| user3       | email6        | 2012-10-08 14:23:53.253 |
+-------------+---------------+-------------------------+
i、 e.我想选择唯一用户列表,以及与之相关的最新电子邮件地址

另一种解释问题的方法是,我想选择一个字段,它不包括在任何聚合函数或ORDERBY子句中。我试过许多语句,它们有很多排列,如Distinct和Order By、Group By等,但都没有用

我试图避免多个陈述


请帮我解决这个问题。

查询背后的想法是在子查询中为每个
quotes\u用户
获取最大发送量
,并将其连接回原始表

SELECT  a.*
FROM    tableName a INNER JOIN
        (
            SELECT  quotes_user, MAX(Sent_AT) maxSENT
            FROM tableName
            Group By quotes_user
        ) b on a.quotes_user = b.quotes_user AND
                a.Sent_AT = b.maxSent
试试这个:

SELECT t2.quotes_user, t2.email_address, t2.Sent_at AS '________Sent_at________'
FROM
(
   SELECT quotes_user, MAX(Sent_at) AS MaxDate
   FROM Table 
   GROUP BY quotes_user
) t1
INNER JOIN Table t2 ON  t1.quotes_user = t2.quotes_user 
                    AND t1.Sent_at = t2.MaxDate

假设最大的发送地址与上次发送的邮件对应。

给出了一个错误:“电子邮件地址”列在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中。很抱歉,更改“GROUP BY quote\U user,email\U adress”的GROUP BY子句应该有效。Works,但没有给出所需的输出。事实上,这是我自己尝试的第一个代码。不起作用,所以经过几次尝试后发布在这里。效果很好。虽然我不知道JOIN部分是如何工作的,但是你能给我介绍一个好的资源吗?@SunnyRGupta-这里有一些资源:。此外,我强烈推荐SQL查询的基础知识。非常感谢,最后一个链接非常有价值:D@SunnyRGupta-随时欢迎你。但是开始读这些,不要扔掉:)和@Mahmoud的答案一样。请避免重复回答。有什么问题吗?你想知道更多什么?
 select quotes_user, email_address, sent_at 
 from table_name t1 inner join
   (select quote_user, max(sent_at) as sent
    from table_name group by quotes_uaser) t2  
 on t1.quotes_user = t2.quotes_user and t1.sent_at = t2.sent
select quote_user, email_adress, max( ___sent_at___ ) from users group by quote_user