为另一列中的每个值获取一列中的相似值的总和
问题描述:
ok, I have a mysql table named "sales_records" like so:
User saleType
+-------+-------+------+
| Jeff | Sale_Closed |
+-------+-------+------+
| Jeff | Sale_null |
+-------+-------+------+
| Sean | sale_closed |
+-------+-------+------+
| Jeff | sale_closed |
+-------+-------+------+
| Sean | sale_closed |
+-------+-------+------+
| Mark | Sale_null |
+-------+-------+------+
| Mark | sale_closed |
+-------+-------+------+
| Josh | sale_closed |
+-------+-------+------+
| Josh | sale_closed |
+-------+-------+------+
I'm trying to count up each saleType for each User.. In other words.. Jeff has 2 "sale_closed" and 1 "sale_null. Mark has 1 "sale_closed" and 1 "sale_null", and so on..
and store it into an array.. (Json) like so.:
{"jeff":{"sale_closed":2,"sale_null":1},"sean":{"sale_closed":2},"Mark":{"sale_closed":1, "sale_null":1},"Josh":{"sale_closed":2}
Ok, so here's my attempt:
<?php
// assuming database connection has already been established..
$result = mysql_query("SELECT DISTINCT `USER' FROM `sales_records`");
while($row = mysql_fetch_assoc($result)) {
$user_array[] = $row['User'];
}
foreach ($user_array as $user) {
$json_array[$user] = mysql_query("SELECT saleType FROM sales_records WHERE user='$user'");
while ($row = mysql_fetch_array($json_array[$user])) { // AND HERE IS WHERE i GET STUCK!!!
$json_array[$user][$username][] = $row['saleType']; // I DON'T KNOW WHERE TO GO FROM HERE...
}
} // AM I APPROACHING THIS CORRECTLY?..
//...
// Once I have all the 'saleType's for each user, maybe I could use:
array_count_values()... or something.
?>
好吧,我有一个名为“sales_records”的mysql表,如下所示: p>
用户saleType
+ ------- + ------- + ------ +
| 杰夫| Sale_Closed |
+ ------- + ------- + ------ +
| 杰夫| Sale_null |
+ ------- + ------- + ------ +
| 肖恩| sale_closed |
+ ------- + ------- + ------ +
| 杰夫| sale_closed |
+ ------- + ------- + ------ +
| 肖恩| sale_closed |
+ ------- + ------- + ------ +
| 马克| Sale_null |
+ ------- + ------- + ------ +
| 马克| sale_closed |
+ ------- + ------- + ------ +
| 乔希| sale_closed |
+ ------- + ------- + ------ +
| 乔希| sale_closed |
+ ------- + ------- + ------ +
code> pre>
我想算一算 为每个用户提供每个saleType ..换句话说..杰夫有2个“sale_closed”和1个“sale_null .Mark有1个”sale_closed“和1个”sale_null“,等等.. p>
并将其存储到数组中..(Json)就像这样。: p>
nn
{“jeff":{"sale_closed":2 ,"sale_null”:1}, “sean”:{“sale_closed”:2},“Mark”:{“sale_closed”:1,“sale_null”:1},“Josh”:{“sale_closed”:2}
code> pre >
好的,所以这是我的尝试: p>
&lt;?php
//假设数据库连接已经建立..
$ result = mysql_query(“SELECT DISTINCT`USER'from`sales_records`”);
while($ row = mysql_fetch_assoc($ result)){
$ user_array [] = $ row ['User'];
} \ n
foreach($ user_array as $ user){
$ json_array [$ user] = mysql_query(“SELECT saleType FROM sales_records WHERE user ='$ user'”);
while($ row = mysql_fetch_array($ json_array) [$ user])){//和这里我去哪里!!!
$ json_array [$ user] [$ username] [ ] = $ row ['saleType']; //我不知道从哪里开始...
}
} //我正确地接近这个问题?..
// ... ...
//一旦我拥有所有的' 每个用户的saleType,也许我可以使用:
array_count_values()......或其他东西。
?&gt;
code> pre>
div>
答
You can get all the data you need in a single query:
SELECT user, saleType, count(*) totalSales FROM sales_record
GROUP BY user, saleType
That will return all the results in 3 columns that should be easy to turn into JSon.