1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > php mysql sum()_PHP MYSQL中的SUM和循环(SUM and Looping in PHP MYSQL)

php mysql sum()_PHP MYSQL中的SUM和循环(SUM and Looping in PHP MYSQL)

时间:2022-08-17 17:05:36

相关推荐

php mysql sum()_PHP MYSQL中的SUM和循环(SUM and Looping in PHP MYSQL)

PHP MYSQL中的SUM和循环(SUM and Looping in PHP MYSQL)

那么,我目前正在使用PHP中的SUM和Looping数组。

这是我的PHP代码:

require_once 'koneksi.php';

if(isset($_POST['id_gejala'])) {

$val = $_POST['id_gejala'];

for($i="0"; $i < sizeof($val); $i++) {

$seldata = mysqli_query($con, "SELECT a.`kode_evidence`, a.`kode_hipotesa`, a.`nilai_evidence`, b.`probabilitas`, SUM(a.`nilai_evidence`) AS `total_evidence` FROM `evidence` AS a INNER JOIN hipotesa AS b ON a.`kode_hipotesa` = b.`kode_hipotesa` WHERE `kode_evidence` ='".$val[$i]."' GROUP BY a.`kode_hipotesa` HAVING COUNT(*) >= 1 ") or die (mysqli_error($con));

while($data1=mysqli_fetch_assoc($seldata)){

$data[]=$data1;

}

}

}

$json = json_encode($data);

echo $json;

?>

我在echo $ json时成功生成了json响应,这里是我得到的响应:

[

{

"kode_evidence":"1",

"kode_hipotesa":"1",

"nilai_evidence":"0.9",

"probabilitas":"0.09",

"total_evidence":"0.8999999761581421"

},

{

"kode_evidence":"2",

"kode_hipotesa":"1",

"nilai_evidence":"0.4",

"probabilitas":"0.09",

"total_evidence":"0.4000000059604645"

}

]

在我的php代码中,sql变量中的GROUP BY和HAVING COUNT命令不起作用,我仍然得到2个响应,结果不会执行nilai_evidence的总和。

我需要从帖子中获得1个结果,并执行查询的SUM计算。

任何帮助将不胜感激,谢谢。

Well, I'm currently working on SUM and Looping array in php.

Here's my php code:

require_once 'koneksi.php';

if(isset($_POST['id_gejala'])) {

$val = $_POST['id_gejala'];

for($i="0"; $i < sizeof($val); $i++) {

$seldata = mysqli_query($con, "SELECT a.`kode_evidence`, a.`kode_hipotesa`, a.`nilai_evidence`, b.`probabilitas`, SUM(a.`nilai_evidence`) AS `total_evidence` FROM `evidence` AS a INNER JOIN hipotesa AS b ON a.`kode_hipotesa` = b.`kode_hipotesa` WHERE `kode_evidence` ='".$val[$i]."' GROUP BY a.`kode_hipotesa` HAVING COUNT(*) >= 1 ") or die (mysqli_error($con));

while($data1=mysqli_fetch_assoc($seldata)){

$data[]=$data1;

}

}

}

$json = json_encode($data);

echo $json;

?>

I've successfully generated the json response when I echo $json, here's the response I get:

[

{

"kode_evidence":"1",

"kode_hipotesa":"1",

"nilai_evidence":"0.9",

"probabilitas":"0.09",

"total_evidence":"0.8999999761581421"

},

{

"kode_evidence":"2",

"kode_hipotesa":"1",

"nilai_evidence":"0.4",

"probabilitas":"0.09",

"total_evidence":"0.4000000059604645"

}

]

In my php code, the GROUP BY and HAVING COUNT command in sql variable are not working, I still get 2 responses and the result won't do the sum of nilai_evidence.

I need to get only 1 result from post and the do the SUM calculation of the query.

Any help will be much appreciated, thank you.

原文:/questions/40553479

更新时间:-10-01 15:23

最满意答案

您只在集合中设置的列的一部分上使用聚合函数。

这可以检索这个列的无关值respctec ggregated函数的值

可能是因为你的查询这些列中的列的价格是相同的

当你可以使用(假)聚合函数来避免返回更多的那一行

"SELECT

min(a.`kode_evidence`)

, a.`kode_hipotesa`

, min(a.`nilai_evidence`)

, min(b.`probabilitas`)

, SUM(a.`nilai_evidence`) AS `total_evidence`

FROM `evidence` AS a

INNER JOIN hipotesa AS b ON a.`kode_hipotesa` = b.`kode_hipotesa`

WHERE `kode_evidence` ='".$val[$i]."'

GROUP BY a.`kode_hipotesa`

HAVING COUNT(*) >= 1 ") or die (mysqli_error($con));

You are using aggregation function only on a part of the column that you set in group by ..

this could retrive unrelated value for this column respctec the value for ggregated function

Could be that for your query the valeue of the column in these column is the same

when you could use (fake ) aggregation function for avoid the return of more that one row

"SELECT

min(a.`kode_evidence`)

, a.`kode_hipotesa`

, min(a.`nilai_evidence`)

, min(b.`probabilitas`)

, SUM(a.`nilai_evidence`) AS `total_evidence`

FROM `evidence` AS a

INNER JOIN hipotesa AS b ON a.`kode_hipotesa` = b.`kode_hipotesa`

WHERE `kode_evidence` ='".$val[$i]."'

GROUP BY a.`kode_hipotesa`

HAVING COUNT(*) >= 1 ") or die (mysqli_error($con));

-11-11

相关问答

sum(1)就是1这个数值,count(1)相当于第一列的名字,虽然统计行结果是一样的,从效率上还是推荐count。显然对于*和name知道主键列的直接指定会更好,不知道的话用星号也无妨。

您显示的代码不可能出现此问题,因此逻辑解释如下: $result = mysql_query($query = "SELECT realname FROM t_user");

if($result)

{

// Something fetched a row from $result before this statement is run

while($data=mysql_fetch_assoc($result)){

echo $data['realname'];

}

}

...

您只在集合中设置的列的一部分上使用聚合函数。 这可以检索这个列的无关值respctec ggregated函数的值 可能是因为你的查询这些列中的列的价格是相同的 当你可以使用(假)聚合函数来避免返回更多的那一行 "SELECT

min(a.`kode_evidence`)

, a.`kode_hipotesa`

, min(a.`nilai_evidence`)

, min(b.`probabilitas`)

...

只需添加初始化变量 $nilaimodul = 0; // try adding this to your code

for($i=1;$i<=$jml_modul;$i++)

{

echo "

".$array_nilai[$nim][$i]."";

$nilaimodul += (array_sum(array($array_nilai[$nim][$i])));

}

$ratarata = ($

...

只需将所有这些人放入数据库并检查如下: <?php

$admins = mysql_query("SELECT * FROM users WHERE id='". mysql_real_escape_string($_SESSION['id']) ."' and is_admin='y'");

?>

此时,如果至少有一行将返回(是的,它应该只返回一行),您可以继续使用专用内容。 just put all those people into the database and check

...

如果你的初学者使用php和mysql,那么下面是你的基础步骤.. 首先:为数据库用户,通过,主机设置配置 $conn = mysql_connect('database_server','database_username','database_password');

SECOND:执行数据库连接。 mysql_select_db($conn,'database name');

第三:创建一个查询(你可以在这里插入你的查询).. $sql= mysql_query("SELECT SUM(To

...

我很确定你在你的SQL中错过了一个组 。 也许改成它 SELECT tbl1.code, tbl1.name, tbl1.cost, tbl1.price, tbl1.vat, sum(tbl2.onhand) as onhand

FROM tbl1

INNER JOIN tbl2 ON tbl1.code=tbl2.code

WHERE (tbl2.name='AC' OR tbl2.name='WH') AND tbl1.code IN ($in)

GROUP BY tbl1.code,

...

我会建议一个更传统的方法: $result1 = mysql_query("SELECT * FROM posts") or die(mysql_error());

while ($row = mysql_fetch_assoc($result1)) {

...

}

另外,不要执行所有这些从属查询。 代替: $sql = <<

SELECT *, (SELECT COUNT(1) FROM pics WHERE postid = p.postid) pic_count

FROM

...

fetchAll返回数组中的所有行。 所有你需要的就是这个 $sql = "select statement";

$stmt = $db->prepare( $sql );

$stmt->execute();

$response = array();

$response["products"] = $stmt->fetchAll( PDO::FETCH_ASSOC );

$response["success"] = 1;

echo json_encode($response);

fetchAl

...

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。