I have column qty in my table, and I want to sum up all the qty from this column in a variable:
$get_sold = "select qty from pending_orders where product_id='$p_id' AND order_status=='done'";
$result = mysqli_query($con, $get_sold);
try this.
$get_sold = "select sum(`qty`) as sum from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_assoc($result);
echo $row['sum'];
First you need sum to your column in your query by using sum(qty)as totals and also remove == from order_status it is =
you need to use mysqli_fetch_row to get your sum data into your variable
<?php
$get_sold = "select sum(`qty`)as totals from `pending_orders` where `product_id`='".$p_id."' AND `order_status`='done'";
$result = mysqli_query($con,$get_sold);
$row=mysqli_fetch_row($result);
$row[0];// get your total sum
try using, MySql sum function
$get_sold = "select sum(qty) total_qty from pending_orders where product_id='$p_id' AND order_status='done'";
$result = mysqli_query($con,$get_sold);
you should use
select sum(qty) as sumqty from pending_orders where product_id='$p_id' AND order_status='done'
Also mention, that in mysql you need to write on equal sign instead of two
Most people have responded here with a quick copy/paste, but I feel like it would be better suited to format your query nicely for readability sake to help you identify the issue.
$get_sold = "
SELECT SUM(`qty`) as `total_quantity`
FROM `pending_orders`
WHERE `product_id` = '$p_id' AND `order_status` = 'done'
";
$result = $con->query($get_sold);
if ( !$result ) {
die("MySQL error: " . $con->error);
}
$row = $result->fetch_assoc();
$quantity = $row['total_quantity'];
CHANE QUERY , SUM(column_name) gives sum of all numbers in column_name
$get_sold = "select SUM(qty) as total from pending_orders where product_id='$p_id' AND order_status='done'";
Related
I would like to update a column row inside MYSQL by adding 50 to the last current number.Means column named sum was 200 and after query, it will update to 250.
I can do this by using two query as follow:
$add1 = 50;
$sql = "SELECT sum FROM table_name WHERE id = '$id' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = mysqli_fetch_assoc($result);
$add2 = $row['sum'] + $add1 ;
}
$sql1 = "UPDATE tabel_name SET sum = '$add2' WHERE id = '$id' ";
$result1 = $conn->query($sql1);
However, is there a better way to do this with less code or by one go?
Thanks
You can calculate values directly in UPDATE queries.
$add1 = 50;
$sql1 = "UPDATE tabel_name SET sum = sum + $add1 WHERE id = '$id';";
In case the type of your column sum is not numeric, but contians a string of the given number, you can use the MySQL CAST function to cast it to a number, calculate it and cast it back to a string again:
$sql1 = "UPDATE tabel_name SET sum = CAST((CAST(sum as INT(11)) + $add1) as VARCHAR(11)) WHERE id = '$id';";
Why dont you try By
$sql1 = "UPDATE tabel_name SET sum = sum + 'YOUR VALUE' WHERE id = '$id'
You can do it using UPDATE for this, just change your query like:
$sql = "UPDATE table_name SET sum=sum+value_you_want WHERE id = '$id' ";
Please use this sql query:
$sql1 = "UPDATE tabel_name SET sum = sum + 50 WHERE id = '$id' ";
I want to get the sum total of the table columns in my database.
I've tried using the following code but have not been successful.
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(value) AS value_sum FROM User_Table');
$row = mysqli_fetch_assoc($result);
$sum = $row['value_sum'];
echo $sum;
Thank you very much!!
I hope you try to find total number of record of a table of User_Table
$link=mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_NAME);
$result = mysqli_query($link,'SELECT SUM(your_column_name) AS value_sum FROM User_Table');
//or like the query for return last row that indicate total number of record
// id auto increment
$result = mysqli_query($link,'SELECT * FROM User_Table ORDER BY id DESC LIMIT 1;');
$row = mysqli_fetch_assoc($result);
$sum = $row['id'];
echo $sum;
// or using count
$result = mysqli_query($link,'SELECT COUNT(*) total_row FROM User_Table;');
$row = mysqli_fetch_assoc($result);
$sum = $row['total_row '];
echo $sum;
As of my understanding you need count the number of columns your database have. If I am not wrong, you may please use the query below
select * from information_schema.columns
where table_schema = '<YOUR DATABASE NAME>'
order by table_name,ordinal_position
Hope this helps. Thanks
I'm trying to return the sum of balances from a table and can get the following code to work when using a specific table name
$qry = mysql_query("SELECT SUM(Balance) AS total FROM table1 ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
The problem I'm having is that my table name changes and this needs to be a variable but when I use the following code I get no result
$table="table1";
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM $table ");
$row = mysql_fetch_assoc($qry);
echo $row['total'];
Can anyone offer some help please?
How about:
$qry = mysql_query(" SELECT SUM(Balance) AS total FROM " . $table );
i need to select one row where slot_left is the biggest. i tried
for ( $i=1;$i<3;$i++) {
$sql5 = "SELECT * from user u where (
select max(slot_left) from company c,user u
where c.id=u.company_id and c.id='$name'
)";
$result5 = mysqli_query($link, $sql5) or die(mysqli_error($link));
while($row=mysqli_fetch_array($result5)) {
// echo the id which the slot_left is the biggest
echo $i['slot_left'];
}
}
but still cannot. please help!
You have to use GROUP BY in your query.
And query execution in Loop is not recommended, it will decrees performance.
Try this.
$sql5 = "select c.id, max(slot_left) from company c,user u
where c.id=u.company_id and c.id='$id' GROUP by c.id";
$result5 = mysqli_query($link, $sql5) or die(mysqli_error($link));
while($row=mysqli_fetch_array($result5)) {
echo $row['slot_left'];
}
SQL can be. You select all rows from DB.
$sql5 = "select max(slot_left) AS slot_left from company c,user u where c.id=u.company_id and c.id='$name' GROUP by u.company_id";
$name variable used in query not set
Variable $i is not array. Array is $row
echo $row['slot_left'];
i have a really complex query (mysql + php) and i would like to know the easiest way to sum columns and rows of a table apart from using SUM(x).
Maybe a javascript could help.
Thanks in advance.
I would advise to use the SUM() function in MySQL. The only reason to not use it is if you have some complicated counting based on other values. Then I would do the counting in the PHP. Here is an example:
$result = mysql_query("... query here ...");
$cats = 0;
$dogs = 0;
while($row = mysql_fetch_array($result))
{
if($row['type'] == 'cat')
{
$cats++;
}
else
{
$dogs++;
}
}
echo "Cats: $cats Dogs: $dogs";
I would do this in the query itself. If you post the query, I can provide more information.
This is the query, i want to sum cols and rows. ( rows are the vars "svago" and "lavoro" ordered for each month of the year, while cols are the same values in the whole year )
$q = "SELECT DISTINCT DATE_FORMAT( TIMESTAMP, '%m%Y' ) AS derp FROM main LIMIT 0 , 30";
$qq = mysql_query($q);
while($res = mysql_fetch_array($qq)) {
$where = $res['derp'];
$q = "SELECT timestamp, SUM(moto) + SUM(mary) AS svago, SUM(lavoro) + SUM(affitto) AS lavoro FROM main WHERE DATE_FORMAT(timestamp, '%m%Y') = $where";