Mysql 按列对项目进行分组,并按其他列排序

Mysql 按列对项目进行分组,并按其他列排序,mysql,Mysql,我有下表,我想为客户进行最新评级 基本上,每当用户更新评级时,计数将增加,并在表中创建一个条目。下表如下 ----------------------------------------------------- |_id| name | client_id | user_id | rating | count | ----------------------------------------------------- |1 | Four | 1 | 1

我有下表,我想为客户进行最新评级 基本上,每当用户更新评级时,计数将增加,并在表中创建一个条目。下表如下

-----------------------------------------------------
|_id| name   | client_id | user_id | rating | count |
-----------------------------------------------------
|1  | Four   |   1       |    1    |   4    |   1   |
|2  | three  |   1       |    1    |   3    |   2   |
|3  | two    |   1       |    1    |   2    |   3   |
|4  | five   |   1       |    1    |   5    |   4   |
|5  | two    |   1       |    2    |   2    |   1   |
|6  | three  |   1       |    2    |   3    |   2   |
|7  | two    |   2       |    1    |   2    |   1   |
|8  | three  |   2       |    1    |   3    |   2   |
-----------------------------------------------------
对于
客户id 1的评级
我希望

-----------------------------------------------------
|_id| name   | client_id | user_id | rating | count |
-----------------------------------------------------
|4  | five   |   1       |    1    |   5    |   4   |
|6  | three  |   1       |    2    |   3    |   2   |
-----------------------------------------------------
到目前为止,我尝试了
SELECT*fromtest
其中client_id=1按client_id分组按count desc排序

但是没有得到预期的结果,有什么帮助吗???

您可以尝试以下方法

select _id, name, client_id, user_id, rating, max(count)
from clients 
group by client_id
试试看


您可以在与相同的表上使用
left join

select t1.* from test t1 
left join test t2 on t1.user_id = t2.user_id 
and t1.client_id = t2.client_id 
and t1._id < t2._id 
where 
t2._id is null 
and t1.client_id = 1 
order by t1.`count` desc;
更新:从注释如何将另一个表连接到上面,这里是一个示例

mysql> select * from users ;
+------+------+
| _id  | name |
+------+------+
|    1 | AAA  |
|    2 | BBB  |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test ;
+------+-------+-----------+---------+--------+-------+
| _id  | name  | client_id | user_id | rating | count |
+------+-------+-----------+---------+--------+-------+
|    1 | four  |         1 |       1 |      4 |     1 |
|    2 | three |         1 |       1 |      3 |     2 |
|    3 | two   |         1 |       1 |      2 |     3 |
|    4 | five  |         1 |       1 |      5 |     4 |
|    5 | two   |         1 |       2 |      2 |     1 |
|    6 | three |         1 |       2 |      3 |     2 |
|    7 | two   |         2 |       1 |      2 |     1 |
|    8 | three |         2 |       1 |      3 |     2 |
+------+-------+-----------+---------+--------+-------+

select t1.*,u.name from test t1 
join users u on u._id = t1.user_id
left join test t2 on t1.user_id = t2.user_id 
and t1.client_id = t2.client_id 
and t1._id < t2._id 
where 
t2._id is null 
and t1.client_id = 1 
order by t1.`count` desc;
请注意,
users
表的连接是内部连接,这将要求在
test
表中的
users
表中预设所有用户


如果
users
表中缺少一些用户,则使用
left join
这将为从
users
表中选择的数据提供空值。

为什么
\u id=5
不应出现在结果中?因为其与用户\u id 2相关的计数为1,所以_id6因为它的计数是2,这是客户端的最大值_id1我尝试了这个没有用,我在问题中也提到了谢谢,我得到了预期的结果,还有其他简单的查询吗?这些是最简单的:)强烈推荐,我想使用user_id从用户表中获取用户信息。我如何使用您的查询来实现这一点,基本上用户是另一个表。只需向用户表添加一个联接,然后选择要从用户表中选择的字段。如果表名是
users
,那么在第一种情况下,它将在第一次左键联接之前作为
join users u on u.user_id=t1.user_id
,在第二种情况下,在
联接(…)
之前它将是相同的,我正在尝试这样选择t1.*,u.*从测试t1在u上连接用户u。_id=t1.user\u id在t1上左连接测试t2.user\u id=t2.user\u id和t1.client\u id=t2.client\u id和t1。_idcount
desc;但是没有得到预期的结果
select t1.* from test t1 
join ( 
  select max(_id) as _id,
  client_id,
  user_id 
  from test 
  where client_id = 1 
  group by client_id,user_id 
)t2 
on t1._id = t2._id 
and t1.client_id = t2.client_id 
order by t1.`count` desc; 
mysql> select * from users ;
+------+------+
| _id  | name |
+------+------+
|    1 | AAA  |
|    2 | BBB  |
+------+------+
2 rows in set (0.00 sec)

mysql> select * from test ;
+------+-------+-----------+---------+--------+-------+
| _id  | name  | client_id | user_id | rating | count |
+------+-------+-----------+---------+--------+-------+
|    1 | four  |         1 |       1 |      4 |     1 |
|    2 | three |         1 |       1 |      3 |     2 |
|    3 | two   |         1 |       1 |      2 |     3 |
|    4 | five  |         1 |       1 |      5 |     4 |
|    5 | two   |         1 |       2 |      2 |     1 |
|    6 | three |         1 |       2 |      3 |     2 |
|    7 | two   |         2 |       1 |      2 |     1 |
|    8 | three |         2 |       1 |      3 |     2 |
+------+-------+-----------+---------+--------+-------+

select t1.*,u.name from test t1 
join users u on u._id = t1.user_id
left join test t2 on t1.user_id = t2.user_id 
and t1.client_id = t2.client_id 
and t1._id < t2._id 
where 
t2._id is null 
and t1.client_id = 1 
order by t1.`count` desc;
+------+-------+-----------+---------+--------+-------+------+
| _id  | name  | client_id | user_id | rating | count | name |
+------+-------+-----------+---------+--------+-------+------+
|    4 | five  |         1 |       1 |      5 |     4 | AAA  |
|    6 | three |         1 |       2 |      3 |     2 | BBB  |
+------+-------+-----------+---------+--------+-------+------+