SQL Query From multiple table sum column and display - php

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;
?>

Related

Update percentage column in database table based on count column of all qualifying rows

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.

Echo number of rows where id=$id

I've tried to echo number of rows where a certain id=$id. This is essentially to count the number of "votes" a person has been receiving on the website. It works like a charm in mysqlworkbench, however the number of rows where this person's id has been inserted into database (through the voting button) won't show up on the webpage. The table name is forslag_stemmer, and it has its primary key= id, and foreign keys brukerid (the user that votes for a certain person) and foreign key forslagid (this is the people who receives votes from the users).
This query works in workbench, but not on the page:
echo "<u><b>Number of votes</u></b>:";
$sql= "SELECT COUNT( * ) FROM
bb.forslag_stemmer WHERE forslagid=$forslagid";
$resultat = $kobling->query($sql);
while($rad=$resultat->fetch_assoc())
{
$forslagid = $rad["forslagid"];
echo $sql;
echo "$resultat->num_rows";
}
I really don't know what to do?
You select one field COUNT( * ) as result of your query. There will be no other fields in a result.
echo "<u><b>Number of votes</u></b>:";
// I added an alias for field
$sql= "SELECT COUNT( * ) as votes_count FROM bb.forslag_stemmer WHERE forslagid=$forslagid";
$resultat = $kobling->query($sql);
$rad = $resultat->fetch_assoc();
// access value by alias
echo $rad['votes_count'];
mysqli_result::num_rows Gets the number of rows in a result
In your Example since you're querying as "select count(*) from table" it always returns Number of rows as "1"
You can get Number of rows in two ways
Method 1:
$sql= "SELECT * from FROM bb.forslag_stemmer WHERE forslagid=$forslagid";
$result = $con->query($sql);
echo $result->num_rows; // prints number of rows found
Method 2:
$sql= "SELECT count(*) as resultcount from FROM bb.forslag_stemmer WHERE
forslagid=$forslagid";
$result = $con->query($sql);
while($row = $result->fetch_assoc()){
echo $row['resultcount']; // prints number of rows found
}

Pick daily data via SQL query

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?

Store maximum number value into database table

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)

Query multiple totals at once in MYSQL PHP

I have a a table in my DB that has multiple columns with numbers I would like to query all the rows in 1 query with separate totals for all rows in each column in my db.
like so
$sql = '
SELECT sum(TOTAL1)
, sum(TOTAL2)
, sum(TOTAL3)
, sum(TOTAL4)
FROM TABLE WHERE ID = '.$ID.'';
it works when I do it with a single column query like this.
$sql = 'SELECT sum(TOTAL1) FROM TABLE WHERE ID = '.$ID.'';
but I can't seem to get it to work for multiples in 1 query does anyone know of a more proper way of doing this instead of in separate queries?
$sql = 'SELECT (sum(TOTAL1)+sum(TOTAL2)+sum(TOTAL3)+sum(TOTAL4)) AS FINALTOTAL FROM TABLE WHERE ID = '.$ID.'';
Use aliases.
Sidenote: Add your WHERE clause to the tested examples I've given below.
SELECT sum(TOTAL1) as total1, sum(TOTAL2) as total2
which if you want to use seperate aliases, is handy if you wish to echo them as different entities.
For example:
$query = mysqli_query($con, "SELECT SUM(col1) as total1,
SUM(col2) as total2,
SUM(col3) as total3 FROM table ");
while($row = mysqli_fetch_array($query)){
echo $row['total1']; // echo'd 125
echo "<br>";
echo $row['total2']; // echo'd 225
echo "<br>";
echo $row['total3']; // echo'd 2000
}
Sidenote: My three columns contained the following:
col1 in 2 rows 25, 100
col2 in 2 rows 25, 200
col3 in 2 rows 1000, 1000
To echo the total of all rows and for example: (and inside the while loop)
$total = $row['total1'] + $row['total2'] + $row['total3'];
echo $total;
Or in one go and as one alias and one row echo'd:
SELECT sum(TOTAL1 + TOTAL2 + TOTAL3) as total FROM table
I.e.:
$query = mysqli_query($con, "SELECT SUM(col1 + col2 + col3) as total FROM table ");
while($row = mysqli_fetch_array($query)){
echo $row['total'];
}
NOTE: DATA TYPE must be same for all fields which you want to SUM
By following query you can have SUM(field1)+SUM(field2)+ SUM(field3) .....n in 1 single field
I used this query for just 2 fields and both are integers
See this is my table definition
create table `siso_area_operativa` (
`id_area_operativa` int ,
`area_operativa` varchar (8),
`responsable` int
);
and this is query which you want
SELECT SUM(a.field1) FROM
(
SELECT
SUM(id_area_operativa) field1
FROM
siso_area_operativa
UNION ALL
SELECT SUM(responsable) field1
FROM
siso_area_operativa ) AS a
Result of this is
SUM(id_area_operativa) + SUM(responsable) in single field
ENJOY !!!!!!!!!!!!

Categories