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
Please I have a table with 10 rows and I will like to sum the first five data.
----------
data
----------
1
2
3
4
5
6
7
8
9
10
and I want the result to be 1+2+3+4+5 = 15
SQL Query (when data is not sorted, and you don't require it to be):
SELECT sum(data)
FROM (SELECT data
FROM myTable
LIMIT 5
) AS subquery;
SQL Query (when data is not sorted, but you require it to be):
SELECT sum(data)
FROM (SELECT data
FROM myTable
ORDER BY data ASC
LIMIT 5
) AS subquery;
http://sqlfiddle.com/#!9/e51db/2
In your SQL query, use limit like LIMIT 0,5 which will limit the results of the table to only the first 5 rows. and sum it.
So do :
SELECT sum(your_column) from `your_table` LIMIT 0,5
If this doesn't help, show how you are pulling the data from MySQL.
Related
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 tried writing the below query but i just need 3 winners(1st,2nd and 3rd place) and my query has just brought every project in descending order but i need only top 3.
<SELECT judge_review.sub_ID, SUM(judge_review.avgPoints) AS TOTAL FROM judge_review GROUP BY judge_review.sub_ID ORDER BY TOTAL DESC>
You need to apply LIMIT 3
SELECT judge_review.sub_ID,
SUM(judge_review.avgPoints) AS TOTAL
FROM judge_review
GROUP BY judge_review.sub_ID
ORDER BY TOTAL DESC
LIMIT 3
Reference:- Limit Data Selections From a MySQL Database
To limit number of returned rows in the result set you have to specify number of that rows by adding to the end of the query LIMIT 3.
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 the follow table structure:
COLUMN1 COLUMN2 COLUMN3
1 6 1
3 3 7
4 8 9
I'd like to get the average of all columns. It's possible to get this with a single SQL command, with PHP prepared statements?
Thanks
I think you want to take the averages of each columns seperately. Above answer works but another answer can be as like below;
SELECT
SUM (COLUMN1)/ COUNT(COLUMN1) AS AVG1,
SUM (COLUMN2)/ COUNT(COLUMN2) AS AVG2,
SUM (COLUMN3)/ COUNT(COLUMN3) AS AVG3
FROM TABLE
SELECT
AVG (COLUMN1) AS AVG1,
AVG (COLUMN2) AS AVG3,
AVG (COLUMN3) AS AVG3
FROM TABLE
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
Just as the title says, I need a query to count the rows until the condition is met. Here's my setup:
partnumber
-----------
b
e
d
a
c
So if I'm going to search for partnumber = d it will sort the part number and return 4 (since d is the 4th when you sort the partnumber).
I can do this inside a loop. I'm just wondering if there is a query for this.
Thanks in advance
SELECT COUNT(*) FROM THE_TABLE WHERE partnumber <= (SELECT ID FROM THE_TABLE WHERE partnumber <= 'd');
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;
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;