I am trying to create a view in MySQL which shows a Player's name, their Guardians name, their Guardians phone number and the team the Player plays in.
I have this script which creates the database :
DROP TABLE IF EXISTS TeamCoach;
DROP TABLE IF EXISTS TeamPlayer;
DROP TABLE IF EXISTS CoachQualification;
DROP TABLE IF EXISTS PlayerGuardian;
DROP TABLE IF EXISTS PersonAddress;
DROP TABLE IF EXISTS PersonPhoneNumber;
DROP TABLE IF EXISTS Coach;
DROP TABLE IF EXISTS Player;
DROP TABLE IF EXISTS Qualification;
DROP TABLE IF EXISTS Team;
DROP TABLE IF EXISTS PhoneNumber;
DROP TABLE IF EXISTS School;
DROP TABLE IF EXISTS Person;
DROP TABLE IF EXISTS Address;
DROP TABLE IF EXISTS Guardian;
DROP FUNCTION IF EXISTS TeamSize;
CREATE TABLE Qualification (
qualificationID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
level int NOT NULL,
PRIMARY KEY (qualificationID)
) ENGINE=InnoDB;
CREATE TABLE Team (
teamID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
ageGroup varchar(30) NOT NULL,
year int NOT NULL,
PRIMARY KEY (teamID)
) ENGINE=InnoDB;
CREATE TABLE Address (
addressID int NOT NULL AUTO_INCREMENT,
number int NOT NULL,
street varchar(30) NOT NULL,
suburb varchar(30) NOT NULL,
townCity varchar(30) NOT NULL,
PRIMARY KEY (addressID)
) ENGINE=InnoDB;
CREATE TABLE PhoneNumber (
phoneNumberID int NOT NULL AUTO_INCREMENT,
number varchar(20) NOT NULL,
PRIMARY KEY (phoneNumberID)
) ENGINE=InnoDB;
CREATE TABLE School (
schoolID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
PRIMARY KEY (schoolID)
) ENGINE=InnoDB;
CREATE TABLE Person (
personID int NOT NULL AUTO_INCREMENT,
name varchar(30) NOT NULL,
email varchar(30) NOT NULL,
photo varchar(30) NOT NULL,
PRIMARY KEY (personID)
) ENGINE=InnoDB;
CREATE TABLE PersonAddress (
personID int NOT NULL,
addressID int NOT NULL,
KEY personID (personID),
KEY addressID (addressID),
FOREIGN KEY (personID) REFERENCES Person (personID),
FOREIGN KEY (addressID) REFERENCES Address (addressID)
) ENGINE=InnoDB;
CREATE TABLE PersonPhoneNumber (
personID int NOT NULL,
phoneNumberID int NOT NULL,
KEY personID (personID),
KEY phoneNumberID (phoneNumberID),
FOREIGN KEY (personID) REFERENCES Person (personID),
FOREIGN KEY (phoneNumberID) REFERENCES PhoneNumber (phoneNumberID)
) ENGINE=InnoDB;
CREATE TABLE Coach (
coachID int NOT NULL PRIMARY KEY REFERENCES Person (personID),
dateBeganCoaching varchar(10) NOT NULL
) ENGINE=InnoDB;
CREATE TABLE Player (
playerID int NOT NULL PRIMARY KEY REFERENCES Person (personID),
DOB varchar(10) NOT NULL,
schoolID int NOT NULL,
KEY schoolID (schoolID),
FOREIGN KEY (schoolID) REFERENCES School (schoolID)
) ENGINE=InnoDB;
CREATE TABLE Guardian (
guardianID int NOT NULL PRIMARY KEY REFERENCES Person (personID)
)ENGINE=InnoDB;
CREATE TABLE PlayerGuardian (
guardianID int NOT NULL,
playerID int NOT NULL,
KEY guardianID (guardianID),
KEY playerID (playerID),
FOREIGN KEY (guardianID) REFERENCES Guardian (guardianID),
FOREIGN KEY (playerID) REFERENCES Player (playerID)
) ENGINE=InnoDB;
CREATE TABLE TeamPlayer (
teamID int NOT NULL,
playerID int NOT NULL,
KEY teamID (teamID),
KEY playerID (playerID),
FOREIGN KEY (teamID) REFERENCES Team (teamID),
FOREIGN KEY (playerID) REFERENCES Player (playerID)
) ENGINE=InnoDB;
CREATE TABLE TeamCoach (
teamID int NOT NULL,
coachID int NOT NULL,
KEY teamID (teamID),
KEY coachID (coachID),
FOREIGN KEY (teamID) REFERENCES Team (teamID),
FOREIGN KEY (coachID) REFERENCES Coach (coachID)
) ENGINE=InnoDB;
CREATE TABLE CoachQualification (
coachID int NOT NULL,
qualificationID int NOT NULL,
KEY coachID (coachID),
KEY qualificationID (qualificationID),
FOREIGN KEY (coachID) REFERENCES Coach (coachID),
FOREIGN KEY (qualificationID) REFERENCES Qualification (qualificationID)
) ENGINE=InnoDB;
DELIMITER //
CREATE FUNCTION TeamSize(Team varchar(30))
RETURNS int
DETERMINISTIC CONTAINS SQL
BEGIN
DECLARE Size int;
SELECT COUNT(*) INTO Size FROM ((
SELECT * FROM TeamPlayer WHERE teamID=(
SELECT teamID FROM Team WHERE name='Red Bulls')))AS TeamSize;
RETURN Size;
END //
DELIMITER ;
And this script which fills it with data :
INSERT INTO Qualification (name, level) VALUES ('Under 7s', '3');
INSERT INTO Qualification (name, level) VALUES ('Under 8s', '1');
INSERT INTO Qualification (name, level) VALUES ('Under 9s', '5');
INSERT INTO Qualification (name, level) VALUES ('Under 10s', '4');
INSERT INTO Qualification (name, level) VALUES ('Under 80s', '10');
INSERT INTO Team (name, ageGroup, year) VALUES ('Blue Hawks', 'Under 7s', '2015');
INSERT INTO Team (name, ageGroup, year) VALUES ('Yellow Dolphins', 'Under 8s', '2013');
INSERT INTO Team (name, ageGroup, year) VALUES ('Red Bulls', 'Under 9s', '2014');
INSERT INTO Team (name, ageGroup, year) VALUES ('Turquiose Turtles', 'Under 10s', '2015');
INSERT INTO Team (name, ageGroup, year) VALUES ('Violet Butterflies', 'Under 80s', '2015');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('6', 'Selwyn Street', 'North East Valley', 'Dunedin');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('6', 'Inverleith Street', 'Woodhaugh', 'Dunedin');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('40', 'Chaucer Street', 'Milton', 'Milton');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('105', 'Inniscort Street', 'Decent Part', 'Cromwell');
INSERT INTO Address (number, street, suburb, townCity) VALUES ('43', 'Chambers Street', 'North East Valley', 'Dunedin');
INSERT INTO PhoneNumber (number) VALUES ('034178669');
INSERT INTO PhoneNumber (number) VALUES ('0272637393');
INSERT INTO PhoneNumber (number) VALUES ('0277147957');
INSERT INTO PhoneNumber (number) VALUES ('0220826217');
INSERT INTO PhoneNumber (number) VALUES ('0800838383');
INSERT INTO School (name) VALUES ('Tokomairiro High');
INSERT INTO School (name) VALUES ('Cromwell College');
INSERT INTO School (name) VALUES ('Otago Boys');
INSERT INTO School (name) VALUES ('Otago Girls');
INSERT INTO School (name) VALUES ('Woodhaugh Rest Palace');
INSERT INTO Person (name, email, photo) VALUES ('Andrew Fletcher', 'gmail#gmail.com', 'puppy.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Sam Bates', 'outlook#outlook.com', 'kitten.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Mason Osbourne', 'yahoo#gmail.com', 'cheetah.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Zara DeMontgomery', 'hola#malware.com', 'elephant.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Reuben Crimp', 'norton#avg.com', 'dolphins.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Joy Gasson', 'example#example.com', 'owl.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Brian Treanor', 'www#www.com', 'whale.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Dale Parsons', 'java#oracle.com', 'swordfish.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Tom Clark', 'GNU#linux.com', 'lion.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jim Beam', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jack Daniels', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('John Snow', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Ned Stark', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Tywin Lannister', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Hodor Hodor', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Joe Bloggs', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('John Doe', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jane Doe', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Lassie Dog', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO Person (name, email, photo) VALUES ('Jake Sully', 'mysql#databases.com', 'monkey.jpg');
INSERT INTO PersonAddress (personID, addressID) VALUES ('1', '3');
INSERT INTO PersonAddress (personID, addressID) VALUES ('2', '4');
INSERT INTO PersonAddress (personID, addressID) VALUES ('3', '5');
INSERT INTO PersonAddress (personID, addressID) VALUES ('4', '3');
INSERT INTO PersonAddress (personID, addressID) VALUES ('5', '2');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('1', '2');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('2', '4');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('3', '1');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('4', '5');
INSERT INTO PersonPhoneNumber (personID, phoneNumberID) VALUES ('5', '3');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('6', '2014');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('7', '2013');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('8', '2012');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('9', '2014');
INSERT INTO Coach (coachID, dateBeganCoaching) VALUES ('10', '1993');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('1', '08/07/1993', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('2', '02/06/1993', '2');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('3', '08/04/1995', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('4', '08/01/1994', '1');
INSERT INTO Player (playerID, DOB, schoolID) VALUES ('5', '25/12/1992', '5');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('11', '06/07/1998', '3');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('12', '06/07/1998', '3');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('13', '06/07/1998', '4');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('14', '06/07/1998', '5');
INSERT INTO Player (playerID, DOB, SchoolID) VALUES ('15', '06/07/1998', '4');
INSERT INTO Guardian (guardianID) VALUES ('16');
INSERT INTO Guardian (guardianID) VALUES ('17');
INSERT INTO Guardian (guardianID) VALUES ('18');
INSERT INTO Guardian (guardianID) VALUES ('19');
INSERT INTO Guardian (guardianID) VALUES ('20');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('16', '1');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('16', '2');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('17', '3');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('17', '4');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('18', '5');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('18', '11');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('19', '12');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('19', '13');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('20', '14');
INSERT INTO PlayerGuardian (guardianID, playerID) VALUES ('20', '15');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('1', '1');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('2', '2');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('3', '3');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '4');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('5', '5');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('2', '11');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '12');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('4', '13');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('3', '14');
INSERT INTO TeamPlayer (teamID, playerID) VALUES ('5', '15');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('1', '6');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('2', '7');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('3', '8');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('4', '9');
INSERT INTO TeamCoach (teamID, coachID) VALUES ('5', '10');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('6', '5');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('7', '4');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('8', '3');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('9', '2');
INSERT INTO CoachQualification (coachID, qualificationID) VALUES ('10', '1');
Is what I am trying to do even possible?
I've attempted to create a query that will work for your data, but this is assuming you only have one guardian for each player (Which you said in your comment that this is not possible). Here is the SQLFiddle if you want to play around with it a bit more.
SELECT p.name, pg.name AS `GuardianName`, pgpn.`number` AS `GuardianNumber`
FROM `Player` play
LEFT JOIN `Person` p ON play.playerID = p.personID
LEFT JOIN `PlayerGuardian` g ON play.playerID = g.playerID
LEFT JOIN `Person` pg ON g.guardianID = pg.personID
LEFT JOIN `PersonPhoneNumber` pgppn ON pg.personID = pgppn.personID
LEFT JOIN `PhoneNumber` pgpn ON pgpn.phoneNumberID = pgppn.phoneNumberID
The problem you're having is, how do you display multiple guardians for each player? Do you want players to show up multiple times for each guardian that player has? Then you have to worry about grouping coding level, and this can be a big hassle.
My recommendation: Use an ORM similar to CakePHP that will do the heavy lifting for you. You set up the relationships between the tables, and it will do the magic of linking them up for you, in multiple efficient queries.
I have two Tables one for Teams one for Players What I am trying to find out is the total head count table, In other words I want to have a count of the total number of teams that have 2 members, the to all number of teams that have 3 members etc
Here is the database structure.
(Sidebar Question: I'm a newbee here: Is there a better way to post the SQL? )
CREATE TABLE `formsfiles`.`Teams` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Teams` (`Name`) VALUES ('Sharks');
INSERT INTO `Teams` (`Name`) VALUES ('Jets');
INSERT INTO `Teams` (`Name`) VALUES ('Fish');
INSERT INTO `Teams` (`Name`) VALUES ('Dodgers');
CREATE TABLE `Players` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
`Team_ID` INT NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jim', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tom', '1');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Harry', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Dave', '2');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Tim', '3');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Trey', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Jay', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Steve', '4');
INSERT INTO `Players` (`Name`, `Team_ID`) VALUES ('Chris', '4');
What I want is a count Team sizes.
I would like to see the following output
Team_Size Count
1 1
2 2
4 1
The simplest way would probably be:
select team_count, count(*) from
(select count(*) team_count from players group by team_id) sq
group by team_count
(Although this won't include teams with no players in them.)
SQLFiddle here.
First, you need the team sizes:
select t.id as teamId, count(p.id) as teamSize
from
`Teams` as t
left join `Players` as p on t.id = p.teamId
group by
t.id;
Notice that this will return the teams with zero players too. If you don't want that, use inner join instead of left join.
Now, use this query as a row source for your final query:
select teamSize, count(teamId)
from (
select t.id as teamId, count(p.id) as teamSize
from
`Teams` as t
left join `Players` as p on t.id = p.teamId
group by
t.id) as a
group by teamSize;
Hope this helps
Just one more thing.
If you have big data sets, this query may hang. So it may be best to create a temp table, index it, and run the query on the temp table:
drop table if exists temp_teamSize;
create temporary table temp_teamSize
select t.id as teamId, count(p.id) as teamSize
from
`Teams` as t
left join `Players` as p on t.id = p.teamId
group by
t.id;
alter table temp_teamSize
add unique index idx_teamId(teamId),
add index idx_teamSize(teamSize);
select teamSize, count(teamId)
from temp_teamSize
group by teamSize;
I wish to know which team in "TX" have not played a game. (In other words Im looking for a selection of records where there is no related record in the many table.)
Here is the SQL:
(Or if You prefer the sql fiddle is here:http://sqlfiddle.com/#!2/14106 )
CREATE TABLE `Team` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
`State` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) );
CREATE TABLE `Games` (
`ID` INT NOT NULL AUTO_INCREMENT,
`Team_ID` INT NULL ,
`Game_Day` DATE NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Team` (`Name`, `State`) VALUES ('Rams', 'TX');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Rockets', 'OK');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Bombers', 'TX');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Yellow Jackets', 'NV');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Wildcats', 'CT');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Miners', 'CO');
INSERT INTO `Team` (`Name`, `State`) VALUES ('Bolts', 'TX');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('2', '2013-03-16');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('2', '2013-01-01');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('3', '2013-04-16');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('5', '2013-02-02');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('4', '2013-02-12');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('6', '2013-01-09');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('6', '2013-01-01');
INSERT INTO `Games` (`Team_ID`, `Game_Day`) VALUES ('3', '2013-05-01');
I should get the result:
ID Name
1 Rams
7 Bolts
SELECT `ID`, `Name` FROM `TEAM`
WHERE `ID` NOT IN (SELECT DISTINCT(`Team_ID`) from `Games`)
AND `State` = 'TX';
SqlFiddle here.
Use an outer join, selecting only those rows that don't match
SELECT t.*
FROM TEAM t
LEFT JOIN GAMES g ON g.team_id = t.id
WHERE t.state = 'TX'
AND g.team_id is null -- return only rows that *don't* join
This this running in SQL Fiddle
Note that using a join will out-perform a sub-query approach, especially when data sets become large.
You can also left-join to the Games table and filter for where there isn't a corresponding Games row. This is usually faster than NOT IN when the tables have a lot of rows:
SELECT Team.ID, Team.Name
FROM Team
LEFT JOIN Games ON Team.ID = Games.Team_ID
WHERE Team.State = 'TX' AND Games.ID IS NULL;
If there isn't a Games row to go with the Teams row, the Games.ID column will be null in the result, so if you filter on Games.ID IS NULL you'll get all the rows where a team has no games.
There's a SQL Fiddle here.
Hope this would help.
select t.ID,t.NAME
FROM Team t
WHERE t.state = 'TX'
AND t.id NOT IN (SELECT g.TEAM_ID FROM Games g)
I have two tables. Teams and Players. What I want to do is create a query that tells me some statistical data about the salary of the largest team. Specifically I want a count of how many players make less than 5K. How many make between 5K and 10K ....in increments of 5K to the max player.
Here is the SQL:
CREATE TABLE `formsfiles`.`Teams` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Teams` (`Name`) VALUES ('Sharks');
INSERT INTO `Teams` (`Name`) VALUES ('Jets');
INSERT INTO `Teams` (`Name`) VALUES ('Fish');
INSERT INTO `Teams` (`Name`) VALUES ('Dodgers');
CREATE TABLE `Players` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
`Team_ID` INT NULL ,
`Salary` INT NUll ,
PRIMARY KEY (`ID`) );
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Jim', '1', '4800');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Tom', '1', '12000');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Harry', '2', '1230');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Dave', '2', '19870');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Tim', '3', '1540');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Trey', '4','7340');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Jay', '4', '4800');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Steve', '4','6610');
INSERT INTO `Players` (`Name`, `Team_ID`, salary) VALUES ('Chris', '4','17754');
Given this data: The Dodgers are the largest team (ID =4)
We would like an output of:
0-5000 1
5000-10000 2
10000-15000 0
15000-20000 1
If this code looks familiar it is because it is an evolution of a problem of a prior problem I posted here. Kindly don't beat me down!
Here is my attempt at this. It uses joins to satisfy the conditions:
select sr.range,
SUM(case when p.salary >= sr.low and p.salary < sr.high then 1 else 0 end)
from Players p join
(select t.id
from Players p join
Teams t
on p.team_id = t.id
group by t.team_id
order by SUM(p.salary) desc
limit 1
) team
on p.team_id = team.id cross join
(select '0-5000' as range, 0 as low, 5000 as high union all
select '5000-10000', 5000, 10000 union all
select '10000-15000', 10000, 15000 union all
select '15000-20000', 15000, 20000
) sr
group by sr.range
order by min(sr.low)
Notice the use of a separate query for the ranges, to be sure that you get rows with a count of 0.
This code will do almost what you want
SELECT 5000 * FLOOR(Salary / 5000), count(*)
FROM Players
WHERE Team_ID = 4
GROUP BY FLOOR(Salary / 5000)
It returns the low border of the range and the number of entries
0 1
5000 2
15000 1
Note that it does not return empty ranges.