簡體   English   中英

如何從MySQL中的用戶費率表獲取用戶明智的列表

[英]How to get user wise list from user rate table in mysql

我想在mysql查詢中明智地獲得用戶列表組

我的桌子如下

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

那么我如何在mysql中進行查詢以獲取以下結果

user_id   price
2         50
3         27
5         10

在上面的示例中,我將對同一user_id的價格求和,列表將顯示為頂部的頂部價格。

有什么想法,我該怎么做在mysql查詢中?

您可以簡單地通過user_id分組並使用內置函數對價格求和。

      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;

以下查詢得出按價格排序的每個用戶的價格總和。 我認為這就是您想要的。 不要忘記使用您自己的表名而不是<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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM