SQL查询以获取唯一条目。查询正确有效吗?

时间:2019-02-16 04:52:13

标签: mysql sql

下面提供了表结构。使用的查询

desc p2p

输出

'auto_id','bigint(20)','NO','PRI',NULL,'auto_increment'
'created_at','datetime','NO','',NULL,''
'expiry_date','datetime','YES','',NULL,''
'last_updated_at','datetime','NO','',NULL,''
'platform','int(11)','YES','',NULL,''
'receiver_id','varchar(255)','YES','',NULL,''
'receiver_mobile_number','varchar(12)','YES','',NULL,''
'registered_user','tinyint(1)','YES','',NULL,''
'sender_id','varchar(255)','NO','MUL',NULL,''
'sender_mobile_number','varchar(12)','NO','',NULL,''
'txn_amount','decimal(19,2)','NO','',NULL,''
'txn_meta_data','json','YES','',NULL,''
'txn_status','int(11)','NO','',NULL,''
'txn_type','int(11)','NO','',NULL,''
'version','int(11)','YES','',NULL,''

我想从表中获取(2,4)中特定sender_id和txn_type的最新10个唯一的receiver_mobile_number。当接收方手机号码重复时,就会出现此问题。例如,如果我有两行具有相同的receiver_mobile_number,则我想要最新的条目,即按created_at desc顶部条目进行排序。

我进行了以下查询

select MAX(txn_type) as txn_type, MAX(txn_status) as txn_status, MAX(sender_mobile_number) as sender_mobile_number, MAX(sender_id) as sender_id, MAX(registered_user) as registered_user, MAX(receiver_id) as receiver_id, MAX(platform) as platform, MAX(last_updated_at) as last_updated_at, MAX(expiry_date) as expiry_date, MAX(txn_amount) as txn_amount, MAX(auto_id) as auto_id, MAX(version) as version,receiver_mobile_number, MAX(created_at) as created_at, MAX(txn_meta_data) as txn_meta_data from p2p where sender_id = :customer_id and txn_status in (2,4) group by receiver_mobile_number order by MAX(txn_type), MAX(txn_status), MAX(sender_mobile_number), MAX(sender_id), MAX(registered_user), MAX(receiver_id), MAX(platform), MAX(last_updated_at), MAX(expiry_date), MAX(txn_amount), MAX(created_at), MAX(txn_meta_data), MAX(auto_id), MAX(version) desc limit 10

1。)查询是否正确?我的确切疑问是在使用max函数。根据我的理解,Max函数将返回已分组在一起的选定列中的最大值,即对于相同的接收方手机号码,如果txn_amount的最新条目为10,而某些条目的txn_amount为500,那么我将得到500,而不是10。如果2 receiver_mobile_number相同,我实际上需要所有具有最新created_at日期的行。

2。)我需要选择表的所有列。我需要为每个人使用Max吗?在这种情况下,有没有更简单的方式使用*?

3。)这是优化查询吗?这可以改善吗?我在sender_id上有索引。

4。)可以进行哪些替代查询?

为便于理解,我创建了结构,样本数据集和查询。请使用以下链接获取在线sql数据库

https://paiza.io/projects/Sm0hqZ-gUXXxSTCeLfwxmA?language=mysql

请问有人可以帮助我解决这些问题吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

根据您在评论中的解释,下面将是查询-

选择* 从p2p t2        INNER JOIN(选择“ receiver_mobile_number”,                           最大(created_at)AS req_date                    从p2p                    GROUP BY receiver_mobile_number)t1                开启t2.created_at = t1.req_date                   AND t2.receiver_mobile_number = t1.receiver_mobile_number;

上面的查询将从具有最新created_at列的表中获取所有记录。

如果您需要从表中获取最近的10个唯一的sender_id和txn_type作为唯一的sender_mobile_number,那么下面将是查询。

SELECT t2.receiver_mobile_number
FROM   p2p t2
       INNER JOIN (SELECT receiver_mobile_number,
                          Max(created_at) AS req_date
                   FROM   p2p
                   WHERE  sender_id = '<seneder_id>'
                          AND txn_type IN ( 2, 4 )
                   GROUP  BY receiver_mobile_number) t1
               ON t2.created_at = t1.req_date
                  AND t2.receiver_mobile_number = t1.receiver_mobile_number
ORDER  BY t2.created_at DESC
LIMIT  10; 

您编写的查询将选择整个表的列的最大值。如果要为该组汇总列,则必须提供group by子句。

希望这会有所帮助