表 t:
字段 A 字段 B 字段 C ;
根据 C 字段进行分组,筛选出 B 字段最小的那一行,实现查询结果为不同 C 中最小的 B 的行的 A 字段集合。
有大佬有解决方案吗
字段 A 字段 B 字段 C ;
根据 C 字段进行分组,筛选出 B 字段最小的那一行,实现查询结果为不同 C 中最小的 B 的行的 A 字段集合。
有大佬有解决方案吗

1 l00t Dec 29, 2020 看不懂…… |
2 wanv1171 Dec 29, 2020 先进行分组,然后用 over 选出每组最小的 B 。然后重新根据这个结果筛选 C IN (SELECT C) AND B IN (SELECT B),然后 GROUP BY C, GROUP_CONCAT(A) |
4 venhal Dec 29, 2020 可以了解一下开窗函数,用 row_number()over()应该就可以满足 |
5 venhal Dec 29, 2020 不过低版本的 mysql 好像不支持开窗函数,如果不支持的话 select t1.a, t2.b, t2.c from t1 join (select c, min(b) as b from t group by c) as t2 ON t1.c = t2.c AND t1.b = t2.b |
6 lybcyd Dec 30, 2020 via Android 子查询吧,先用 c 分组,用 min 函数查出最小的 b 值,作为一个表 x,然后再查询大表中 b 和 c 等于 x 中 b 和 c 的记录 |
8 liquorppp OP |
9 l00t Dec 30, 2020 两个售价一样低的你打算怎么整。 |
10 weiwenhao Dec 30, 2020 select * from logs INNER JOIN (select MIN(id) as min_id FROM logs GROUP BY user_id) test on `test`.min_id = logs.id 类似这样? |
11 ZanderNg Dec 30, 2020 select t1.* from (select t.a, t.b, t.c, row_number() over(partition by t.a order by to_number(b)) as rank from tgxt.test_a t where t.c is not null) t1 where rank = 1 |
14 xdwmxx Dec 30, 2020 select * from (select t.A, t.B, t.C, row_number() over(partition by t.C order by t.B) rn from TEST_ROW_NUMBER_OVER t) where t.B is not null and rn = 1; 这样吗? |