我的桌子是,我知道桌子很糟糕但很旧: http://sqlfiddle.com/#!9/f2680/1
+----+-----------------------+----------+------+
| ID | COUNTRY | QUANTITY | EACH |
+----+-----------------------+----------+------+
| 1 | U.S.A | 1 | 12 |
| 2 | U.K. | 2 | 3 |
| 3 | GERMANY | NULL | 3 |
| 4 | FRANCE;GERMANY; U.S.A | 0 | 7 |
| 5 | U.S.A;GERMANY | 3 | 8 |
| 6 | GERMANY;FRANCE;U.S.A | 1 | 10 |
+----+-----------------------+----------+------+
<?php
include 'bd_cnx.php';
$ret =[['Test','Valori livrare']];
$sql = "SELECT SUBSTRING_INDEX(COUNTRY, ';', 1) AS COUNTRY,
SUM(COALESCE(IF(QUANTITY = NULL OR QUANTITY = 0,1,QUANTITY),1) * EACHCOL) AS PRICE
FROM table1
GROUP BY COUNTRY";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$ret[] =[$row['COUNTRY'], floatval($row['PRICE'])];
}
}
echo json_encode($ret);
?>
我想对COUNTRY列中的结果值进行分组(使它们成为唯一的),然后生成SUM并最终生成下一个表单:FRANCE = 3; GERMANY = 18; U.S.A = 19。我将在谷歌图表中以下一种形式介绍这些行:['country','value']。 谢谢!
答案 0 :(得分:1)
在第一个查询中使用此查询和其他选择...分组:
select COUNTRY, sum(PRICE) from(
SELECT
SUBSTRING_INDEX(COUNTRY, ';', 1) AS COUNTRY,
SUM(COALESCE(IF(QUANTITY = NULL OR QUANTITY = 0,1,QUANTITY),1) * EACHCOL) AS PRICE
FROM table1
GROUP BY COUNTRY) as t
group by COUNTRY;