How to get sum for the values inside specific column? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a table "selled" which has three column id, qty, code how to sum all the values in qty column where code = X, I know how to count the number of rows in this column but I want to get the sum of the values inside these rows.
I am sorry guys i am still beginner and i really appreciate it ,This is the code i am using and it still not working
$query="SELECT SUM (qty) as sum_of_qty from selled WHERE code='$code'";
$result=mysqli_query($connection,$query);
$row=mysqli_fetch_assoc($result);
echo $row['sum_of_qty'];

Use the SUM function. Docs:
https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_sum
SELECT SUM(qty) as sum_of_qty FROM selled WHERE code = x

Related

How to count some number on a while loop and order them from the biggest number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a shop, and in seller section I want to make a top8 sellers.
Accounts are saved on another table, users on another table.
I want to get users, and count their sold data, and order them like top 8 list goes.
How can I do that, what I need? I have no idea at all.
The database has columns of accounts data and their price, so I need to count the price first.
First of all you need to select all orders you have.
After that, you loop through them and sum their orders by user id:
$sums = array();
foreach($orders as $order){
if(!isset($sums))
$sums[$order['id_user']] = 0;
$sums[$order['id_user']] += $order['order_value'];
// order_value is sum of total products - meaning order value - I', not
}
Now, you have $sums array filled with total spent by user_id.
Now all you have to do, is to sort them usint arsort ( http://php.net/manual/en/array.sorting.php )
Now you have array, where first 8 rows are top buyers.

Selecting average value from multiple rows [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have 2 columns in mysql database i.e. totalRating and ratingDate. There exists multiple values of totalRating against on date e.g. there can be 5 totalRatings on date 2016-08-29, 4 on 2016-08-30. I am using ChartJs to show a graph of totalRating and ratingDate. I want to take the average of totalRatings of a single date and plot it on graph i.e. one totalRating for one date. I am using PHP for the backend. Can somebody help me with the queries?
Using below query you can find average rating for all dates.
select AVG(totalRatings) as avgRating, date from YOUR_TABLE group by date;
I think what you need is the AVG mysql function, your query should be something like:
select AVG(totalRatings) from YOUR_TABLE where date=YOUR_DATE;

Get the closest value of a column in MYSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table that have a CP value (numeric), for example, 28030, 28060, 27100 etc. And the user can introduce a number via PHP. I want to, having this number for example, 28050, order in MYSQL my table putting 28060 as the first position.
This is the basic of my table:
SELECT * FROM `tiendas` ORDER BY `CP`
ABS() will work. Here's a query that does the job:
SELECT
CP
FROM tiendas
ORDER BY ABS(CP- 28050) ASC

How to calculate the sum of the datatable column in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a DataTable which has 5 columns:
sl, Doctor, Appointments, fees
The DataTable contains 4 rows.
I want to show the sum of the fees Column as "Total fees"
What is the best way of doing this?
If you are using DataTable plugin to show tables, then try it to calculate sum
var table = $('#example').DataTable();
table.column( 4 ).data().sum(); // as fees is your fourth column
More Info:
http://www.datatables.net/plug-ins/api/sum()#
https://datatables.net/examples/plug-ins/api.html
For normal database operation try simple query using SUM aggregate method
SELECT SUM(fees) as 'Total fees' FROM your_table_name
You need use the SUM() function. If your table name is DataTable this is your query:
SELECT SUM(fees) as 'Total fees' FROM DataTable
Read more at:
http://www.tutorialspoint.com/mysql/mysql-sum-function.htm

Need a little guidance on counting and calculating sum of rows. [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm making a little voting tool (voting from 1 to 5) and I have the votes stored in a DB. I'm wondering, what is the best manner to count all the rows (of a specific post ID) and then calculate the sum of those specific rows so that I can display the results.
i.e
Results: 3.5 / 5
(total votes: 1047)
Should I use a PHP loop or MYSQL sum?
SELECT AVG(vote), COUNT(vote) FROM vote_table WHERE post_id = 1234;
The first. column gives your average out of 5 and the count the total votes. To get all the stats at once:
SELECT AVG(vote), COUNT(vote), post_id FROM vote_table GROUP BY post_id;

Categories