编程语言
首页 > 编程语言> > php-获得最高数量,产品/用户数

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

作者:互联网

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个结果:

首先通过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

其次是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对ard = 100的prd = 30出价,而对amt = 100 ..的user_id = 2出价,但是user = 4对prd =出价30,因此他是prd = 30的赢家(相同的amt)

尝试通过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

对于第二个user_id,我没有得到什么查询.

谢谢

更新:

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

但经过更多输入后,我发现此错误:第二个http://www.sqlfiddle.com/#!9/e04063/1:对于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

user_id为null(但在我的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

标签:sum,mysql,php,group-by,highest
来源: https://codeday.me/bug/20191025/1929754.html