获取最高数量,产品/用户数量
Get Highest amount , count of products / users
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 个结果 table :
首先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 出价 prd=30 和 user_id =2 以 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
我没有得到要查询的内容。
谢谢
更新:
感谢 HARSHIL:http://www.sqlfiddle.com/#!9/5325a6/5/1
但在输入更多内容后我发现了这个错误:第二个 http://www.sqlfiddle.com/#!9/e04063/1:user_id 但对 [= 效果很好68=](第一次查询)
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
with null user_id(但在我的 wamp 服务器中,我在 winner_on_bid_prd_id 中看不到 user_id =1 的 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;
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 个结果 table :
首先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 出价 prd=30 和 user_id =2 以 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
我没有得到要查询的内容。
谢谢
更新:
感谢 HARSHIL:http://www.sqlfiddle.com/#!9/5325a6/5/1
但在输入更多内容后我发现了这个错误:第二个 http://www.sqlfiddle.com/#!9/e04063/1:user_id 但对 [= 效果很好68=](第一次查询)
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
with null user_id(但在我的 wamp 服务器中,我在 winner_on_bid_prd_id 中看不到 user_id =1 的 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;