3
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 对 prd=30 出价 amt =100 和 user_id =2 with amt=100 ..但第一个出价是 user=4 在 prd= 30 因此他是 prd=30 的获胜者(对于相同的 amt )

尝试了下面的查询,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,我没有得到将要查询的内容。

谢谢

更新 :

谢谢哈希尔: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

但我想要如下:

没有 null 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
4

1 回答 1

4

对于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;

单击此处获取更新的演示

于 2017-10-30T18:08:54.303 回答