php - 获取最高数量,产品/用户数量

标签 php mysql group-by sum highest

id | user_id | prd_id | amnt | dis 
1  |   1     |  10    |  200 | 23
2  |   2     |  10    |  300 | 11
3  |   3     |  20    |  100 | 26
4  |   2     |  20    |  50  | 12
5  |   4     |  30    |  100 | 22
6  |   2     |  40    |  600 | 18
7  |   2     |  30    |  100 | 16

我想要上表中的 2 个结果:

首先 by prod_id 如下

prd_id |  user_id  | cont |   highestamt | disc
10     |   2       |  2   |   300        | 11
20     |   3       |  2   |   100        | 26
30     |   4       |  2   |   100        | 22
40     |   2       |  1   |   600        | 18

第二个 by user_id 如下:

user_id | cont |  bid on prd_id    | winner on bid prod_id |  
1       |  1   |   10              |  -                    |   -
2       |  4   |   10,20,30,40     |  10,40                |
3       |  1   |   20              |  20                   |
4       |  1   |   30              |  30                   |

更新:例如:以上: user_id = 2 对产品 10,20,30,40 出价(对 prd_id 出价)因此他的出价 cont = 4 ...并且他是10,40 的获胜者(出价 prod_id 的获胜者)..为什么只有 10,40 而不是 30 ...bcz user_id =4 以 amt =100 和 user_id =2 与 amt=100 出价 prd=30 ..但首先由 user=4 在 prd=30 出价,因此他是 prd=30 的赢家(对于相同的金额)

尝试了下面的 by prd_id 查询,但它给了我一些错误的结果。

SELECT `prd_id`, `user_id` , count('prd_id') as cont , max(`amnt`) as highestamt,disc
FROM `proddtails` 
group by `prd_id` order by `prd_id`

以上查询结果如下:(user_id,disc不正确)

prd_id |  user_id  | cont |   highestamt | disc
10     |   2       |  2   |   300        | 11
20     |   2       |  2   |   100        | 11
30     |   2       |  1   |   100        | 11
40     |   2       |  1   |   600        | 11

第二个 by user_id 我没有得到查询的内容。

谢谢

更新:

感谢 HARSHIL:http://www.sqlfiddle.com/#!9/5325a6/5/1

但在更多输入之后我发现了这个错误:http://www.sqlfiddle.com/#!9/e04063/1第二个:for user_id 但适用于 prd_id(第一个查询)

user_id  cont   bid_on_prd_id   winner_on_bid_prod_id
1         1         10                  (null)
2         4     10,20,40,30            10,40,30
3         1        20                     20
4         1        30                     30

但我想要如下:

没有空 user_id

user_id  cont   bid_on_prd_id   winner_on_bid_prod_id
2         4     10,20,30,40             10,40
3         1        20                     20
4         1        30                     30

使用 null user_id(但在我的 wamp 服务器中,对于 user_id =1,我在 winner_on_bid_prd_id 中看不到 null,我得到值 10 而不是 null)

user_id  cont   bid_on_prd_id   winner_on_bid_prod_id
1         1         10                  (null)
2         4     10,20,30,40             10,40
3         1        20                     20
4         1        30                     30

最佳答案

对于 prd_id:

select t1.prd_id,t1.user_id,
 (select count(*) from tablename where prd_id = t1.prd_id)as cont,
t1.amnt as highststatment,
t1.dis as disc
from tablename t1
where (t1.prd_id,t1.amnt) in
(select prd_id, max(amnt) from tablename group by prd_id)
group by t1.prd_id;

对于 usr_id:

    select t1.user_id,
       count(*) as cont,
       Group_concat(t1.prd_id separator ',') as bid_on_prd_id,
       (select Group_concat(distinct t2.prd_id separator ',')
        from tablename t2 
        where t2.user_id = t1.user_id 
        and (t2.id) in 
                    (select min(id) from tablename
                        where (prd_id,amnt) in 
                                 (select prd_id,max(amnt) from tablename group by prd_id)
                      group by prd_id
                     )
         ) as winner_on_bid_prod_id
from tablename t1
group by t1.user_id
having winner_on_bid_prod_id IS NOT NULL;

Click here for UPDATED DEMO

关于php - 获取最高数量,产品/用户数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47021489/

相关文章:

php - 如何最好地构造这个MySQL数据库

php - Scandir 无法打开目录

mysql - <= 在mysql查询中显示<的记录

java - 与hibernate分组的其他字段一起计数

mysql - 按 X 天分组

mysql - 查询统计数据库以获取不同事件的计数

php - Windows 操作系统中的 chmod 功能与 Linux 中不同

php - 如何知道 RentalCars api 拾取和下拉时间,因为它以 24 位二进制格式返回响应

MySQL 多行更新查询

mysql组内最大计数