i have this code that makes a sum of 2 tables
<div align="right"> Total Vanzari ziua curenta: <i><strong>
<?php
$query = "SELECT (SELECT SUM(totaldeplata) FROM vanzari WHERE
datainregistrarii >= CURRENT_DATE()) + (SELECT SUM(totaldeplata) FROM
players WHERE datainregistrarii >= CURRENT_DATE()) as result";
$query_run = mysql_query($query);
$row = mysql_fetch_assoc($query_run);
$sum = $row['result'];
echo "sum of two different column from two tables : "+ $sum;
?></strong> </i> Lei
</div>
it display a sum of 2 row from 2 different tables
is there a way to store this maximul value into database (its actually the daily sale so the maximum value need to be stored into a table row)
Related
I want to update the percentage column based on the count data for all is_enabled = 1 rows in a table.
My coding attempt looks like this:
<?php
$total = '';
$result= mysqli_query($conn, "SELECT SUM(count) FROM My_Databse WHERE is_enabled ='1'");
while($row = mysqli_fetch_array($result)){
$total = $row['SUM(count)'];
}
$percentage = '';
$result= mysqli_query($conn, "SELECT * FROM My_Database WHERE is_enabled ='1' ORDER BY count DESC");
while($row = mysqli_fetch_array($result)){
$percentage = ($row[2] / $total) * 100;
echo '<div class="progress">';
echo '<div class="progress-bar" role="progressbar" aria-valuenow="'.$percentage.'" aria-valuemin="0" aria-valuemax="100" style="width:'.$percentage.'%">';
echo $row[1].'('.round($percentage).')';
echo '</div>';
echo '</div>';
$i++;
}
$result = mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = 1");
?>
I have now the problem, that I always get the last percentage. How can I update the percentage for every row?
Update query must be within while loop, after calculating percentage -
mysqli_query($conn, "UPDATE My_Database SET percentage = ".$percentage." WHERE id = ". $row['id'] );
I do not recommend making multiple trips to your database, nor using php for a task that can be simply, efficiently, directly, and completely done with a single query.
Join a derived table containing the count total for all qualifying rows, then build the arithmetic to calculate the percentage and update the rows accordingly.
It is more efficient to join the derived table versus calling the subquery for each qualifying row.
Code: (DB-Fiddle)
UPDATE my_table
JOIN (SELECT SUM(`count`) total FROM my_table WHERE is_enabled = 1) all_enabled
SET percentage = ROUND(`count` / total * 100)
WHERE is_enabled = 1;
New table data:
id
tutorial
count
is_enabled
percentage
1
House
3
1
6
2
Car
34
1
68
3
Tuna Fish
22
0
0
4
Bike
13
1
26
Depending on your circumstances (how often this table is read and written to), you might rather declare a TRIGGER to auto calculate&update the percentage column whenever count or is_enabled values are changed or a new row with is_enabled is INSERTed.
I have a problem, I'm trying to make a sum of a daily data from a specific table and column
<?php
$query = "SELECT (SELECT SUM(totaldeplata) FROM vanzari WHERE
MONTH(datainregistrarii) = MONTH(CURRENT_DATE())) + (SELECT SUM(totaldeplata)
FROM players WHERE MONTH(datainregistrarii) = MONTH(CURRENT_DATE())) as result";
$query_run = mysql_query($query);
$row = mysql_fetch_assoc($query_run);
$sum = $row['result'];
echo "sum of two different column from two tables : "+ $sum;
?>
This php query pick the monthly data and make a sum of it, it works, but I want to be able to make also a sum of daily data.
Any idea how?
i have a database with 2 different tables
One table (players) contain a column nammed "totaldeplata" that contains numbers (price)
Other table (vanzari) contain a column like the first one nammed "totaldeplata" it hase the same value inside numbers (the price)
i manage to display the sum of all column for each table like this:
<div align="right"> Total Comenzi luna curenta: <i><strong>
<?php
$query = "SELECT * FROM vanzari WHERE MONTH(datainregistrarii) =
MONTH(CURRENT_DATE())";
$query_run = mysql_query($query);
$qty= 0;
while ($num = mysql_fetch_assoc ($query_run)) {
$qty += $num['totaldeplata'];
}
echo $qty;
?></strong> </i> Lei
</div>
But this, display only the sum of one table, so i have to write the same code to display the sum of other column in the secound table (in this case the players table)
I need to sum all the numbers from columns "totaldeplata" from tables: vanzari and players, and display them as one number (to sum all the numbers and display them)
Actualy Sum numbers from columns "totaldeplata" from both tables "vanzari" and "players" and display them.
Any ideea how to do that? :)
SELECT (SELECT SUM(field1) FROM table1) + (SELECT SUM(field2) FROM table2) as result
According to your above code and table name.
<?php
$query = "SELECT (SELECT SUM(totaldeplata) FROM vanzari) + (SELECT SUM(totaldeplata) FROM players) as result";
$query_run = mysql_query($query);
$row = mysql_fetch_assoc($query_run);
$sum = $row['result'];
echo "sum of two different column from two tables : "+ $sum;
?>
All i am doing is saving user ratings and reviews to table and then calling it from database. Now what I want to do is add all the ratings (like: 5+4+3= 12) and then divide them by count of the ratings (12/3 = 4) and get average rating or aggregate rating.
I am able to display the reviews but how can I add all values from rating column and get average value.
if (mysqli_num_rows($mesult) > 0) {
$count = mysqli_num_rows($mesult);
echo '<p class="">'.$count.' Reviews</p>';
while($row = mysqli_fetch_assoc($mesult)) {
$name =$row["name"];
$text =$row["text"];
$heading =$row["heading"];
$rating =$row["rating"];
echo '<div class="review" itemscope itemtype="http://schema.org/Review"> <meta itemprop="itemReviewed" content ="'.$title.'"/> <p class="heading"><strong>'.$heading.'</strong></p><div class="ratings" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating"> <meta itemprop="worstRating" content = "1"/><span itemprop="ratingValue">'.$rating.'</span>/<span itemprop="bestRating">5</span></div><p class="reviewtext" itemprop="description">'.$text.'</p><div class="reviewby" itemprop="author" itemscope itemtype="http://schema.org/Person"><span itemprop="name">'.$name.'</span></div></div>';
}
Also what I am querying is
$loutput ="SELECT * FROM rating
WHERE product=$ID";
$mesult = mysqli_query($conns,$loutput);
You can also use MySQL's AVG() to calculate the average rating:
SELECT AVG(rating) AS avgRating FROM myTable;
This is how you round the result to X decimals:
SELECT ROUND(AVG(rating), X) AS avgRating FROM myTable;
$loutput ="SELECT AVG(Column_name) as avg FROM rating WHERE product='".$ID."'";
$mresult=mysqli_fetch_assoc(mysqli_query($loutput));
echo $mresult['avg'];
Use aggregate function
to get the average use the avg()
SELECT avg(rating) from rating;
I'm really stuck and have been searching for a while, but with no success.
Anyway, I have this formula known as the Bayesian Estimate: (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C
And I have three database tables: persons, reviews, ratings.
The persons table is quite basic, but for the sake of this question, it only has one field: ID
The reviews table has id, personID, description where personID is the ID of the person.
The ratings table has id, personID, reviewID, ratingX, ratingY, ratingZ where person is the ID of the person and reviewID is the ID of the review. ratingX/Y/Z are three different ratings for the person, and in my page shows the average of the three numbers.
THE FORM THAT LISTS THEM, however, sorts them by the Bayesian Estimate formula. I do not know how to do this and it seems beyond me, since you cannot ORDER BY $bayesian_formula or anything like that. The script looks something like this:
<?php
$result = $db->query("SELECT * FROM persons");
$m =; //SQL to get average number of reviews of all persons
$c =; //SQL to get average rating of all persons
while( $row = $result->fetch_array() ){
$r =; //SQL to get average ratings of person
$v =; //SQL to get total number of reviews of person
$formula = ($v / ($v+$m)) * $r + ($m / ($v+$m)) * $c;
$result2 = $db->query("SELECT * FROM ratings ORDER BY $formula");
while( $row2 = $result2->fetch_array() ){
$result3 = $db->query("SELECT * FROM persons WHERE id='$row2[person]'");
$row3 = $result3->fetch_array();
echo $row2['description']."<br> rating: ".round( ($row['ratingX'] + $row['ratingY'] + $row['ratingZ']) / 3 );
}
}
?>
$formula loops to the database result's weighted rating each iteration.
Obviously that isn't correct. How would I make this work? Would I have to revise my entire script? The actual one is much longer and detailed.
edit:
sqls are:
$c_query=$db->query("SELECT ((ratingX + ratingY + ratingZ) / 3) as avg_rate FROM ratings");
$c_ = $c_query->fetch_array();
$c = $c_['avg_rate'];
$m_query=$db->query("SELECT COUNT(id) AS count FROM ratings");
$m_ = $m_query->fetch_array();
$m = $m_['count'];
$v_query=$db->query("SELECT COUNT(id) AS count FROM ratings WHERE person='$row[id]'");
$v_ = $v_query->fetch_array();
$v = $v_['count'];
$r_query=$db->query("SELECT ((ratingX + ratingY + ratingZ) / 3) as avg_rate FROM ratings WHERE person='$row[id]'");
$r_ = $r_query->fetch_array();
$r = $r_['avg_rate'];
You can use CREATE PROCEDURE