Here's what I have and here's what I'm trying to accomplish:
teams TABLE
TeamID Season Coach1 Coach2
1 2011 35 22
2 2011 27
3 2012 11
4 2013 22 13
staff TABLE
StaffID Nickname
11 Bob
13 Rick
22 Mary
27 Steve
35 Joe
desired OUTPUT:
TeamID Season c1 c2
1 2011 Joe Mary
2 2011 Steve
3 2012 Bob
4 2013 Mary Rick
Here's my current MYSQL query:
SELECT gt.TeamID, gt.Season, gt.Coach1, c1.Nickname c1Nickname, gt.Coach2, c2.Nickname c2Nickname
FROM gladiator_teams as gt, staff as c1, staff as c2
WHERE gt.Coach1=c1.StaffID AND gt.Coach2=c2.StaffID
This returns:
TeamID Season c1 c2
1 2011 Joe Mary
4 2013 Mary Rick
I can't figure out how to modify the query to return rows that have NULL values for Coach 2. I suppose I need an IFNULL function to go somewhere, but I can't figure out where exactly. Any help is greatly appreciated!
You should use left JOIN
SELECT
gt.TeamID
, gt.Season
, gt.Coach1
, c1.Nickname c1Nickname
, gt.Coach2
, c2.Nickname c2Nickname
FROM gladiator_teams as gt
LEFT JOIN staff as c1 gt.Coach1=c1.StaffID
LEFT JOIN staff as c2 gt.Coach2=c2.StaffID
Related
I have two tables, one with forecast production weights and one with actual production weight.
A customer can and will have multiple types and multiple seasons, and will also invoice those types over the year
Table Estimates
Customer Type Weight Season
John A 10 2018
John A 20 2018
John B 10 2018
Bill A 10 2018
Bill C 10 2017
Robert B 30 2017
Robert C 10 2018
Table Actual
Customer Type Weight InvoiceDate
John A 5 2018-10-30
John A 5 2018-10-30
John A 5 2018-10-30
John C 10 2018-10-30
Bill A 5 2018-11-1
Bill C 10 2017-11-30
Bill C 10 2017-11-30
Bill C 10 2017-11-30
Robert B 30 2017-11-10
Robert C 10 2019-2-20
Desired Query Would be as follows
select customer,
type,
sum(weight),
sum(weight)
from
estimates,
actual
where
season = 2018 and
InvoiceDate between 2018-7-1 and 2019-6-30 and
estimates.type = actual.type and
estimates.customer = actual.customer
group by
customer,
type
This give wildly large numbers
Desired result would be selecting for 2018
Customer Type Sum(Estimate) Sum(Actual)
John A 30 15
John B 10 0
John C 0 10
Bill A 10 5
Robert C 10 10
I have tried several join and union queries attempting to solve this issue
I cant quite get my head around which join to use to get the desired result
You can try below way -
select A.customer,A.type, estimated,actual
from
(
select customer,
type,sum(wieght) as estimated
from estimate where season=2018 group by customer,type
)A inner join
(
select customer,
type,sum(wieght) as actual
from actual where InvoiceDate between '2018-7-1' and '2019-6-30' group by customer,type
)B on A.customer=B.customer and A.type=B.type
I am new to this site and I need the help for following
person table:
driver_id name address
1 sankar karaikal
2 vivek chennai
3 kumaran pondy
4 siva chennai
car table
license model year
22 bmw 2014
23 toyata 2014
24 audi 2015
25 maruti 2014
owns table
driver_id license
1 22
2 23
3 24
4 25
Question is how to find who owned cars that were involved in accident in 2014.
I need a sql query for this and thanks in advance.
select name from person p
inner join
owns o on
(p.driver_id=o.driver_id)
inner join
car c on (c.license=o.license)where year='2014';
i want to get the max(HA) and max(Total) of grouped by 'subject' for all students
for the given table
Date Sem Section Subject USN Name Status HA Total
-----------|------|----------------------------------------------------------------
2013-11-11 1 A c 01 Akshay present 1 2
2013-11-11 1 A c 02 David absent 1 2
2013-11-11 1 A .net 01 Akshay absent 1 2
2013-11-11 1 A .net 02 David present 2 2
2013-11-12 1 A c 01 Akshay absent 0 1
2013-11-12 1 A c 02 David present 1 1
2013-11-12 1 A .net 01 Akshay present 1 1
2013-11-12 1 A .net 02 David present 1 1
REsult should be:
Sem Section Subject USN Name HA Total
-----|--------|---------|------|--------|-----|------
1 A c 01 Akshay 1 2
1 A c 02 David 1 2
1 A .net 01 Akshay 1 2
1 A .net 02 David 2 2
Pls.. help...
select sem,section,subject,usn, Name, max(HA), MAX(total)
from table
group by sem, section, subject, usn, naam
I need help with a sql statement.
table users:
user_id name
----------------------
1 joe
2 sam
5 tammy
6 jade
10 tony
table a1:
id user_id year approved
----------------------------------
1 5 2012 1
2 6 2012 0
3 6 2013 1
4 5 2013 1
5 10 2012 0
6 10 2013 0
I need to return all 5 user_id wither or not they have an entry in table a1 and I need fields: a1.id, users.user_id, users.name, a1.year, a1.approved from the join.
My attempt:
select a1.id, users.user_id, users.name, a1.year, a1.approved from users
LEFT JOIN a1 as a1 ON a1.user_id = users.user_id
Im stuck.
Thanks
With a table of:
id | name | job | rank
01 john teacher 4
02 mark teacher 2
03 phil plummer 1
04 dave teacher 7
05 jim plummer 9
06 bill plummer 2
How can I select up to 2 rows of each job (if possible sorted by rank ASC in each group, so that the lowest two ranking of each group get picked). The result I'd be looking for is:
02 mark teacher 2
01 john teacher 4
03 phil plummer 1
06 bill plummer 2
This basically groups by job, with a limit to 2 and sorted by rank. I've been trying with GROUP BY as well as LEFT JOIN, but I just can't figure out how to do this. When creating a "temporary list" of jobs with GROUPING BY job, how do I join more than once onto that job?
SELECT id, name, job, rank
FROM TableName a
WHERE
(
SELECT COUNT(*)
FROM TableName as f
WHERE f.job = a.job AND
f.rank <= a.rank
) <= 2;
SQLFiddle Demo