[英]MySQL - SELECT IN - Counting the # of rows for a user_id in a result?
I'm basically trying to query a table to see who might be gaming my paypal system. 我基本上试图查询一个表,看看谁可能正在游戏我的paypal系统。
The idea is I query my table to see what payments have failed, and then total up the number of failures by user_id
. 我的想法是查询我的表以查看哪些付款失败,然后通过user_id
累计失败次数。 Pretty straightforward concept, I'm just really struggling on how to do it. 非常简单的概念,我真的很挣扎如何做到这一点。
My start query: 我的开始查询:
SELECT * FROM `paypal_ipn` WHERE initial_payment_status = 'Failed'
I then want to use the above query, and just print out a total per payer_id
(a column). 然后我想使用上面的查询,并打印出每个payer_id
(一列)的总数。
Is this possible in MySQL alone or do I need to use a PHP script? 这在MySQL中是否可行,或者我是否需要使用PHP脚本?
You basically want to do a sql GROUP BY
operation. 你基本上想要做一个SQL GROUP BY
操作。 I couldn't tell from the question whether the data needs to be grouped by a column called user_id
or payer_id
. 我无法从问题中看出数据是否需要按名为user_id
或payer_id
的列进行分组。 One of these queries should work for you: 其中一个查询应该适合您:
SELECT user_id, count(*) FROM `paypal_ipn`
WHERE initial_payment_status = 'Failed'
GROUP BY user_id;
SELECT payer_id, count(*) FROM `paypal_ipn`
WHERE initial_payment_status = 'Failed'
GROUP BY payer_id;
这个怎么样:
Select payer_id, count(paypal_tranaction_id) from paypal_ipn where initial_payment_status = 'Failed' group by payer_id
You just need to group by on user_id
. 您只需要在user_id
上进行分组。 So the query would be look like: 所以查询看起来像:
SELECT user_id, count(*) FROM paypal_ipn WHERE initial_payment_status = 'Failed' GROUP BY user_id;
. 。
You can do something like this; 你可以这样做; I've added the total of failed payments to show you other interesting things you can query (I don't know if the payment is really called payment
, mind). 我已经添加了失败的付款总额,以向您展示您可以查询的其他有趣的东西(我不知道付款是否真的称为payment
,请注意)。
SELECT payer_id,
COUNT(*) AS transactions,
SUM(payment) AS total
FROM `paypal_ipn` WHERE initial_payment_status = 'Failed'
GROUP BY payer_id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.