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;
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 6 years ago.
Improve this question
I have a table that holds prices, the idea behind it is to have users suggest prices they think are reasonable for a good,example; range
You can divide the price by the range size, and then use FLOOR() to get the beginning of the range:
SELECT 1500*FLOOR(price/1500) AS price_base, COUNT(*) AS count
FROM yourTable
GROUP BY price_base
ORDER BY count DESC
For example, price_base = 3000 contains all prices from 3000 to 4499, while price_base = 4500 contains all prices from 4500 to 5999.
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 7 years ago.
Improve this question
Is it possible to calculate the average of data retrieved from mysql database on basis of date. For example records are saved in database on daily basis with storing its date the average method will calculate overall average but can it be possible to calculate on basis of date stored in database
You can group records by date and calculate average of a column.
Example:
SELECT date, AVG(column_name)
FROM tbl_name
GROUP BY 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 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,....