[英]MySQL Query select count where in group by
I have this data: 我有此数据:
| bid_id | created | auction_id | user_id | bid_credits | bid_credits_free | bid_rating | balance | bidded_price | last_for_user | bid_ip | bid_type |
+--------+---------------------+------------+---------+-------------+------------------+------------+---------+--------------+---------------+--------------+----------+
| 735 | 2013-10-11 10:02:58 | 9438 | 62323 | 1 | 0 | 0.0000 | 100333 | 0.86 | Y | 72.28.166.61 | single |
| 734 | 2013-10-11 10:02:56 | 9438 | 76201 | 1 | 1 | 0.0000 | 1115 | 0.85 | Y | 72.28.166.61 | single |
| 733 | 2013-10-11 10:02:55 | 9438 | 62323 | 1 | 0 | 0.0000 | 100334 | 0.84 | N | 72.28.166.61 | single |
| 732 | 2013-10-11 10:02:54 | 9438 | 76201 | 1 | 1 | 0.0000 | 1116 | 0.83 | N | 72.28.166.61 | single |
| 731 | 2013-10-11 10:02:52 | 9438 | 62323 | 1 | 0 | 0.0000 | 100335 | 0.82 | N | 72.28.166.61 | single |
I'm trying to get the number of "bid_credits" and "bid_credits_free" as SEPARATE VALUES... 我正在尝试将“ bid_credits”和“ bid_credits_free”的数量作为单独的值...
So the query should return me: 所以查询应该返回我:
| user_id | count(bid_credits) | count(bid_credits_free) |
+---------+--------------------+-------------------------+
| 62323 | 3 | 0 |
| 76201 | 2 | 2 |
The query that I am using is: 我正在使用的查询是:
select user_id, count(bid_credits), count(bid_credits_free) from bids_history
where auction_id = 9438 and user_id in (62323,76201) group by user_id;
but it's not counting the bids correctly... Any ideas? 但它没有正确计算出价...有什么想法吗?
Thanks 谢谢
use a sum instead of count when grouping.. should work 分组时使用总和而不是计数。
I also reformatted so its easier to read :) 我也重新格式化,因此更容易阅读:)
SELECT
user_id,
SUM(bid_credits),
SUM(bid_credits_free)
FROM bids_history
WHERE auction_id = 9438 AND user_id IN (62323,76201)
GROUP BY user_id;
the reason why you want to use a sum instead of count is the count will just count the number of rows in a table, but not the contents / value of whats inside it. 之所以要使用总和而不是计数,是因为计数只会计算表中的行数,而不是表中内容的内容/值。 so when you group by an id like that you need to do a sum to see the actual addition of the contents.
因此,当您按这样的ID分组时,您需要进行总和才能看到内容的实际添加。 hope that helps explain things a bit :)
希望可以帮助解释一下:)
You're looking to SUM them, COUNT is just counting rows. 您想对它们求和,COUNT只对行进行计数。 Try this:
尝试这个:
select user_id, sum(bid_credits), sum(bid_credits_free) from bids_history
where auction_id = 9438 and user_id in (62323,76201) group by user_id;
Try this query : 试试这个查询:
Please use SUM instead of COUNT... 请使用SUM而不是COUNT ...
SELECT user_id, SUM(bid_credits) AS bid_credits, SUM(bid_credits_free) AS bid_credits_free FROM bids_history WHERE auction_id = 9438 AND user_id IN (62323,76201) GROUP BY user_id; SELECT user_id,SUM(bid_credits)AS bid_credits,SUM(bid_credits_free)AS bid_credits_free FROM bids_history中的bid_id = 9438和user_id IN(62323,76201)GROUP BY user_id;
I think this may help you 我认为这可能对您有帮助
select user_id, sum(bid_credits) AS bidCreditsCount, sum(bid_credits_free) AS bidCreditsFreeCount from bids_history
where auction_id = 9438 and user_id in (62323,76201) group by user_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.