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
Related
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 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.
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 about 30 000 records in database, and I must create 10 cron jobs for these:
job 1 fetches records where column `id` is 0,10,20,30, etc.
job 2 fetches records where column `id` is 1,11,21,31, etc.
and so forth.
How can I approach this?
Use the modulo operator
select * from your_table
where id % 10 = 1
to get 1,11,21,31,... and so on. And
where id % 10 = 2
for 2,12,22,32,....
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 9 years ago.
Improve this question
My table is this:
CLASS
5
5
6
6
6
7
8
8
8
9
10
Is it possible to retrieve class values 5,6,7,8,9,10 without retrieving duplicate class values?
SELECT DISTINCT CLASS FROM MYTABLE;
You can use DISTINCT to achieve it.
Syntax:
SELECT DISTINCT column_name FROM table_name;
Based on above syntax, your query should be
SELECT DISTINCT CLASS FROM 'table_name'
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;