如何从表mysql中获取特定colom的特定行的总和?

时间:2017-07-01 13:25:28

标签: php mysql sum calculated-columns

这里我试图从mysql中的表的特定列中获取15行中的5个特定行。

$sql = "SELECT SUM(column) FROM Table WHERE business_name= 'my business'";
$result = $conn-> query($sql);
$row = $result -> fetch_assoc();

print_r ($row);

Array ( [SUM(rating)] => )  

2 个答案:

答案 0 :(得分:1)

尝试Count(*)

$sql = "SELECT COUNT(*) AS 'column' FROM Table WHERE business_name= 'my business'";

答案 1 :(得分:0)

您必须使用别名。 $sql = "SELECT SUM(column) as ss FROM Table WHERE business_name= 'my business'";然后使用$row['ss']

获取数据
$sql = "SELECT SUM(column) as ss FROM Table WHERE business_name= 'my business'";
$result = $conn-> query($sql);
$row = $result -> fetch_assoc();
echo $row['ss'];

我希望它有所帮助!