简体   繁体   中英

How to get user wise list from user rate table in mysql

I want to get user list group wise in mysql query

My table is like below

id    user_id    price
1     3          25
2     2          45
3     5          10
4     2          5
6     3          2

So how can i make query in mysql to get following result as

user_id   price
2         50
3         27
5         10

In the above example, i am going to sum of price for same user_id and listing will be showing as top price on the top.

Is there any idea, how can i do it in mysql query?

you can simply groupby user_id and sum the price using builtin function.

      SELECT totprice, user_id         
      FROM
(
    SELECT SUM(price) as totprice, user_id         FROM tablename 
    GROUP BY user_id         
) as table1
       ORDER BY 
totprice 
SELECT user_id,sum(`price`) as price FROM table GROUP BY user_id

尝试这个

Select distinct user_id, sum(price) from yourtable group by user_id;

The following query results in the total SUM of the price from each user ordered by price. I think this is what you were looking for. Don't forget to use you own table name instead of <table_name>

SELECT `user_id` as 'user',
    (SELECT SUM(`price`) AS price FROM `<table_name>` WHERE `user_id` = `user` GROUP BY `user_id`) AS 'price'
FROM `<table_name>`
GROUP BY `user_id`
ORDER BY `price` DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM