mysql distinct max_在SQL里, 我该如何SELECT行和MAX( Column值) , DISTINCT 另一列?_mysql_酷徒编程知识库...

最快速的MySQL解决方案,不需要内部查询和GROUP BY :SELECT m.* # get the row that contains the max value

FROM topten m #"m" from"max"

LEFT JOIN topten b #"b" from"bigger"

ON m.home = b.home # match"max" row with"bigger" row by `home`

AND m.datetime < b.datetime # want"bigger" than"max"

WHERE b.datetime IS NULL # keep only if there is no bigger than max

说明:

使用home列对表进行Join操作,使用LEFT JOIN确保表m中的所有行都出现在结果集中,在表b中没有匹配项的那些对象为b中的列提供NULL s。

JOIN上的另一条条件要求只匹配b中的行,该行的值比中的行更大。

+------------------------------------------+--------------------------------+

| the row from `m` | the matching row from `b` |

|------------------------------------------|--------------------------------|

| id home datetime player resource | id home datetime ... |

|----|-----|------------|--------|---------|------|------|------------|-----|

| 1 | 10 | 04/03/2009 | john | 399 | NULL | NULL | NULL | ... | *

| 2 | 11 | 04/03/2009 | juliet | 244 | NULL | NULL | NULL | ... | *

| 5 | 12 | 04/03/2009 | borat | 555 | NULL | NULL | NULL | ... | *

| 3 | 10 | 03/03/2009 | john | 300 | 1 | 10 | 04/03/2009 | ... |

| 4 | 11 | 03/03/2009 | juliet | 200 | 2 | 11 | 04/03/2009 | ... |

| 6 | 12 | 03/03/2009 | borat | 500 | 5 | 12 | 04/03/2009 | ... |

| 7 | 13 | 24/12/2008 | borat | 600 | 8 | 13 | 01/01/2009 | ... |

| 8 | 13 | 01/01/2009 | borat | 700 | NULL | NULL | NULL | ... | *

+------------------------------------------+--------------------------------+

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL中,row_number()函数本身并不能实现去重的功能。row_number()函数是用来为查询结果集中的每一分配一个唯一的号,通常用于排序或分页查询中。如果要实现去重的功能,可以使用distinct关键字或者group by子句来实现。 使用distinct关键字可以对查询结果集进去重。例如,使用以下语句可以去除结果集中的重复SELECT DISTINCT column1, column2, ... FROM table_name; 另一种方法是使用group by子句来分组并去重。例如,使用以下语句可以按照某一列分组并去重: SELECT column1, column2, ... FROM table_name GROUP BY column1; 需要根据具体的需求选择合适的方法来实现去重。希望能帮到您!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mysql 数据去重的三种方式[实战]](https://blog.csdn.net/m0_55070913/article/details/126113153)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Mysql学习SQLdistinct 和 row_number() over() 的区别及用法](https://blog.csdn.net/weixin_39751871/article/details/114784218)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值