按列分组项目和按其他列分组

时间:2015-04-17 06:42:10

标签: mysql

我有如下表格,我想为客户提供最新评级 基本上用户每当更新评级,计数将增加并且将在表格中进行输入。表格如下

-----------------------------------------------------
|_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   |
-----------------------------------------------------

对于client_id 1的评分,我希望像

一样
-----------------------------------------------------
|_id| name   | client_id | user_id | rating | count |
-----------------------------------------------------
|4  | five   |   1       |    1    |   5    |   4   |
|6  | three  |   1       |    2    |   3    |   2   |
-----------------------------------------------------

到目前为止,我试过SELECT * FROM test where client_id = 1 group by client_id order by count desc; 但没有得到预期的结果,任何帮助?

3 个答案:

答案 0 :(得分:2)

您可以在与

相同的表格上使用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;

使用不相关的子查询,您可以这样做

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  |
+------+-------+-----------+---------+--------+-------+------+

请注意,users表的连接是内连接,这将要求在users表格中预设所有用户test

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

答案 1 :(得分:0)

您可以尝试类似

的内容
select _id, name, client_id, user_id, rating, max(count)
from clients 
group by client_id

答案 2 :(得分:0)

试试吧

SELECT * FROM test
where client_id = 1 
group by user_id 
order by count desc