I have a site where users selects which team will win a sports match
What I want to do
I want to count the number of times each team was selected
Example:
5 users selected manchester united, 3 users selected liverpool, 8 users selected Chelsea and so on
My problem
I am struggling to find the correct query to execute the above
I have tried the following
SELECT pick, COUNT(pick) AS pickNR FROM multiple_picks WHERE round = '3' AND tournament ='Super RUgby' GROUP BY round_game_nr
But it returns incorrect results as can be seen from the image below, since not each team has been selected 6 times.
My Table format can be seen from the image below
If anyone can advise me what I am doing wrong, or point me in the right direction it would be greatly appreciated. The end goal is to get the number of times each team was selected, and add it to a php variable and insert it into a chart library, but I just need to get the query right.
You are selecting a different column which is not in GROUP BY clause. You are grouping by round_game_nr and selecting pick. I hope pick is the team name.
SELECT pick, COUNT(round_game_nr) AS pickNR
FROM multiple_picks WHERE round = '3' AND tournament ='Super RUgby'
GROUP BY pick
Related
I'm a begginer in MySQL and have been searching for a solution for this problem for a few hours now. I found a few answers, but can't seem to implement them.
Basically, heres what I want to do:
I want to find how a user from from a given country would rank compared to other users from the same country when they are ranked by ID
For example:
Joe is ID 10 and is from the US
There are 100 users from the US
Result: Joe ranks 10th
How would I convert this to an MySQL query?
Thank you in advance.
Create 2 user variables,eg,running & previous which gets the previous rank number and accordingly calculate the rank number of the country by incrementing the current value by 1.
select u.id,u.country,u.rank
from (
select u1.id,u1.country,
#running:=if(#previous=concat(u1.id,u1.country),#running,0) + 1 as rank,
#previous:=concat(u1.id,u1.country)
from tablename u1
order by concat(t.id,t.country)
) u;
Replace tablename with your table
So I'm having some problems with an SQL Query, or maybe rather deciding on if I could solve this issue faster in an PHP While loop. See I have a small table with 4 teams, and each team will be assigned a rank based on each teams total points in GOLF,FOOTBALL and Miniature GOLF. The points in the different games: 8 to the first placed team, then 6->4->2 to the other teams depending on score in each game. Then I convert the total score to a ranking number. Here is an example:
MY question and my problem is:
That I don't want two teams to share rank. As you can see team_id 3 and 2 have the same amount of total points, therefor they have rank 3 together. I want to order this table after the team with the highest value in any of the games that have been played. SO! Team 2 has 8 points in one of the games, which is higher then team 3 highest score. So the rank_13 should look like this instead:
I tried to make a query that compares two teams with the same rank_12 point, but I don't know how to compare on 3 columns. Also tried to print the first table out in PHP and then alter the displayed values depending on highest value in the 3 columns, even more confusing. Am I unclear somehow? Please give me a comment.
you can try to order several column seperated by commas and use GREATEST() to find maximum points and compare like this
......order by rank_12 ASC,GREATEST(golf_points,football_points,mini_points)DESC
I've got myself into a bit of a tiss over averaging and joining tables.
Essentially I want to display the average heights of different plant species using Highcharts, pulling the data from a MySQL database. Unfortunately the height data and the species names were setup to be added in different tables.
I've got it working, however when I download the data and find the averages in Excel the figures are different to those being displayed - so I'm obviously not doing it right. I've double checked I'm doing it right in Excel so almost certain it's my MySQL query that's stuffing up.
There's loads of entries in the actual tables, so I've just put an example below.
The query I have at the moment is:
<?php
$result = mysql_query("
SELECT DISTINCT(plant_records.plant_id), ROUND(AVG(plant_records.height),2) as plant_average, plant_list.id, plant_list.plant_species
FROM plant_records
INNER JOIN plant_list
ON plant_records.plant_id=plant_list.id
GROUP BY plant_list.plant_species
") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$xAxisValues[] = "'" . $row['plant_species'] . "'";
$AseriesValues[] = $row['plant_average'];
}
?>
Am I doing it right? I found some nice tutorials explaining joins, like this one, but I'm still confused. I'm wondering if I'm averaging before I've joined them, or something??
"plant_id" in the Records table corresponds with "id" in the List table
plant_records:
id plant_id date_recorded height
1 3 01/01/2013 0.2523123
2 1 02/01/2013 0.123
3 3 03/02/2013 0.446
4 3 04/03/2013 0.52
5 1 05/03/2013 0.3
6 2 06/03/2013 0.111
7 2 07/05/2013 0.30
8 4 08/05/2013 0.22564
9 1 09/05/2013 1.27
10 3 10/05/2013 1.8
plant_list:
id registration_date contact_name plant_species plant_parent
1 01/01/2013 Dave ilex_prinos London_Holly
2 02/01/2013 Bill acer_saccharum Brighton_Maple
3 01/01/2013 Bob ilex_prinos London_Holly
4 04/01/2013 Bruno junip_communis Park_Juniper
EDIT:
I've tried every possible way of finding the data using Excel (e.g. deliberately not filtering unique IDs, different average types, selecting multiple species, etc) to find the calculation my query is using, but I can't get the same results.
I notice two issues with your query at the moment.
Selecting plant_list.id while having a GROUP BY plant_list.plant_species will not yield anything of interest, due to the fact that MySQL will return an arbitrary id from any of the plants that match each species.
You state that you are only interested in the most recent recording, but nothing in your query reflects that fact.
Given that information, try this query:
SELECT ROUND(AVG(pr.height),2) as plant_average, plant_list.plant_species
FROM plant_records pr
INNER JOIN plant_list
ON pr.plant_id=plant_list.id
WHERE pr.date_recorded = (
SELECT MAX(pri.date_recorded) FROM plant_records pri
WHERE pri.plant_id = pr.plant_id
)
GROUP BY plant_list.plant_species
Alternately, if you want just the average heights for a specific date, simply pass that directly into the query, instead of using the subquery.
If we are assuming that plant_id is not the unique identifier - meaning that a single plant_id is only for one single plant of any given species and you want to know what the average height of a single species is you can do this:
SELECT PL.plant_species, ROUND(AVG(PR.height),2) as plant_average
FROM plant_records AS PR
JOIN plant_list AS PL
ON PR.plant_id=PL.id
GROUP BY PL.plant_species
This will return something like:
plant_species plant_average
acer_saccharum 0.2100000
ilex_prinos 0.6700000
junip_communis 0.2300000
I want to work out an average "rating" for my cocktails in a table. There is a tblRating linked to tblCocktail and within this table is "value" and "counter". "value" contains the 1-5 rating a user can give each cocktail, and "counter" contains the value "1". I want to work out the average rating for each cocktail by adding up all the "value"s and dividing it by the number of "counter"s
I've never done mathematical calculations based on php fields before so any help is appreciated
-matt
You probably do not need the counter field, the AVG function can calculate averages without requiring a counter:
SELECT AVG(value)
FROM tblRating
WHERE cocktail_id = 1234
Notes:
You can count the number of rows inside the rating table to determine the number of ratings
NULL values are not taken into account when average is calculated
You must stick with your chosen scale and not allow values outside the 1-5 range
Here's an exaple that should do what you want, obviusly without having the full table schema it is not possible being more accurate, but the example should show the idea :
SELECT c.name, AVG(r.value)
FROM tblCocktail c
JOIN tblRating r ON c.id = r.cocktail.id
GROUP BY c.id, c.name
Basically you are selecting all cocktail from tblCocktail and calculating the average of the rating(r.value).
I'm currently working on a small feature for a project and wondered how best to achieve the result, i have a table full of reviews with the score being out of 5 and being stored as score in the database, on one page i want to show how many of each score reviews there are, ie number of 5 star reviews, 4 star etc
But i don't know how best to achieve surely i don't need 5 different queries, that would be awful design would it not ?
Thanks and hope you can help !
Since I do not have your table structure, I would do something similar to this (with appropriate names replaced)
edited SQL based on comments
Select Score, COUNT (*) as NumScores
From MyTableOfScores
Group By Score
Order by Score Desc
You need something like this:
select score, count(*) from reviews group by score