For a hockey website, I would like to display past results from the home team and of course display the name of the away team. I could make it almost to the end, but I can't the away team name to show up. I receive all needed results from the RESULTS table. In there I have the home_team_id and the away_team_id, which I need to connect to the TEAM table in order to show the team name. I could do this with the home team but have no idea how to get the away team names of those found in there.
results table
teams table
This is what shows me everything, but no away team name:
SELECT results.home_team_id
, results.away_team_id
, results.ft_score
, teams.name
FROM results
LEFT
JOIN teams
ON results.home_team_id = teams.team_id
WHERE results.home_team_id = '$hteam‘"
I have check other post with similar problems/question, but they did not help me. Sorry.
Help would be great!
Thank you very much!
You need to join the teams table twice with different alias names
SELECT results.home_team_id, results.away_team_id,
results.ft_score,
thome.name as home_name, taway.name as away_name
FROM results
LEFT JOIN teams thome ON results.home_team_id = thome.team_id
LEFT JOIN teams taway ON results.away_team_id = taway.team_id
WHERE results.home_team_id = '$hteam'
Related
I am setting up a program for contacts. I want to keep track of the contact and their email and phone number and what company they work for if we have that readily available. I will not always have the company they work for though so I set it up line this:
tblContacts
ContactID
ContactName
ContactPhoneNumber
tblCompanies
CompanyID
CompanyName
tblContactsInCompanies
CICID
CompanyID
ContactID
I did the ContactsInCompanies because not all of the contacts would have a company assigned. Now I am trying to get all of the contacts with the company name they are in if it is available and I cannot get the Joins to work.
Since I am having so much trouble, I thought I would ask if that is the way it should be set up or not (would anyone recommend a different setup). If that is OK, how would I get the list of all contacts and the company name if it is assigned? I have been trying to LEFT JOIN and INNER JOIN but I just get an error unless all are INNER JOIN but then it doesn't show contacts that do not have a company.
I was finally able to get a combination that worked.
SELECT Contacts.*, Companies.CompanyName
FROM Companies RIGHT JOIN (Contacts LEFT JOIN ContactsInCompanies ON
Contacts.ContactID = ContactsInCompanies.ContactID) ON
Companies.CompanyID = ContactsInCompanies.CompanyID;
These always seem to confuse me. :( Thanks for looking.
I have 3 tables "Employees", "EmployeeLeaveDays" and "EmployeeLeaves".
I'm looking to create a view that displays the date of the leave and the employee name. So in order for my calendar to work I have split everyones leave into individual days(EmployeeLeaveDays) which has an FK that links each day back to (EmployeeLeaves) which has other details around the leave, in EmployeeLeaves I have a column "employee" which is an FK back to employees which contains the name.
So In my view I want to return the name as you can see is 2 tables away, I've wrote this MySQL query but it doesn't work (returns no data), I'm wondering if there is anyway to do what I need to do?
SELECT
EmployeeLeaveDays.id,
EmployeeLeaveDays.employee_leave,
EmployeeLeaveDays.leave_date,
EmployeeLeaveDays.leave_type
FROM EmployeeLeaveDays
INNER JOIN EmployeeLeaves
ON EmployeeLeaveDays.employee_leave=EmployeeLeaves.employee
INNER JOIN Employees
ON EmployeeLeaves.employee=Employees.employee_id;
Hopefully from that you're able to see what I'm trying to achieve, how ever I've attached some screenshots of the table structure.
Thanks
After some thinking I got there in the end. Here's the final query.
SELECT
EmployeeLeaveDays.id,
EmployeeLeaveDays.employee_leave,
EmployeeLeaveDays.leave_date,
EmployeeLeaveDays.leave_type,
EmployeeLeaves.employee,
Employees.employee_id,
Employees.first_name,
Employees.last_name
FROM EmployeeLeaveDays
LEFT JOIN EmployeeLeaves ON EmployeeLeaveDays.employee_leave = EmployeeLeaves.id
LEFT JOIN Employees ON EmployeeLeaves.employee = Employees.id;
I have 4 tables which need to be joined. They are:
Contractors
Crews
Skill_type
Location
I also need to be able to count the number of contractors in each crew.
I have created some SQL (MySql) which does the job nicely:
Select contractors.crew_id as contractors_crew_id,
auburntree.crews.crew_name, count(*) as members, skill_type.skill,
location.location_name
FROM contractors
JOIN auburntree.crews on contractors.crew_id=crews.id
JOIN skill_type on skill_type.skill_id = crews.skill_id
JOIN location on location.id = crews.location_id
GROUP BY contractors.crew_id ;
The contractors table contains a foreign key reference to which crew they are assigned to. Hence the column "contractors_crew_id"
I get a nice result, as you can see in this screen capture image:
https://drive.google.com/file/d/0B5elaUk7GlRoS0JWblE3ZzFXY0E/view?usp=sharing
Problem Defined:
I need to define an empty crew first before I add contractors/members. I will give it a name, a skill and a location. I might define an empty crew several days in advance. Currently an empty crew does not show up in my results.
I want to see:
contractors_crew_id = Null, crew_name, skill_type, Location, members 0
I have tried using RIGHT OUTER JOIN on my tables and I still do not see empty crew. LEFT OUTER JOIN does not work either.
contractors_crew_id will be Null at that point, as no contractors have been assigned yet.
I Hope this makes, sense - sorry it is a bit long and complicated. I have tried really hard to explain in.
Many Thanks !!
Because you are actually focussing on information about crews, and not contractors, I would suggest to start by querying the crews table (in the FROM part of your query) and then join the other tables.
Using a LEFT JOIN on the contractors table should then give you the desired result. Since crews is now the "left" table in the query, the result will include all rows from crews, even when there is no corresponding contractor. This answer explains it very well.
This query works for me on test tables based on your examples:
SELECT
contractors.crew,
crews.crew,
COUNT(contractors.id) as members,
skills.skill,
locations.location
FROM crews
JOIN skills ON crews.skill = skills.id
JOIN locations ON crews.location = locations.id
LEFT JOIN contractors ON contractors.crew = crews.id
GROUP BY contractors.crew, crews.id;
To see for yourself, try this SQL Fiddle.
Try this, instead:
Select contractors.crew_id as contractors_crew_id, crews.crew_name, count(*) as members, skill_type.skill, location.location_name
FROM crews
LEFT OUTER JOIN contractors on contractors.crew_id=crews.id
LEFT OUTER JOIN skill_type on skill_type.skill_id = crews.skill_id
LEFT OUTER JOIN location on location.id = crews.location_id
GROUP BY contractors.crew_id ;
That should pull all crews, regardless of whether or not they have contractors assigned. The way you wrote your original query starts by looking at your contractors and then pulling in any crews they might be assigned to. Crews is clearly your focal table here, so you should be joining the rest of your tables to it, rather than to the contractors table.
I'm a bit rusty on SQL and just need a little help thinking this through.
Let's say I have tables for Applications, Ratings and Admins. The idea is that each of the Admins can mark down their Rating for each Application. A Rating has foreign keys for admin_id and application_id.
For the query, I'd like to select all Applications that any particular Admin has not yet rated. Thoughts?
A simple LEFT JOIN perhaps? It basically just returns all rows where there exists no rating from the admin with the particular id.
SELECT a.*
FROM applications a
LEFT JOIN ratings r
ON a.application_id = r.application_id
AND r.admin_id = ?
WHERE r.admin_id IS NULL
I'd write a fiddle, but SQLfiddle is tired again.
SELECT ap.application_id FROM application ap,rating r,admin ad WHERE ad.admin_id=r.admin_id AND ap.application_id NOT IN (SELECT DISTINCT application_id FROM rating) AND ad.admin_id=5;
Does this give the answer?
Haven't tried it myself though, but i think it will..
I would like to pull up a catalog of games, but I also want to know if a game is in a SPECIFIC member's (current member in session) list of games already. This information is stored in a table called gameslist, which is a joiner table that joines members and games gameslists(id,memberid,gameid,rank)
SELECT DISTINCT *, gameslists.memberid
FROM games
INNER JOIN platforms ON games.platformid = platforms.id
INNER JOIN titles ON games.titleid = titles.id
INNER JOIN genres ON games.genreid = genres.id
LEFT OUTER JOIN gameslists ON games.id = gameslists.gameid
WHERE platforms.id = 1
ORDER BY games.releasedate DESC
LIMIT 8
PROBLEM if a game is in the list of two members, it shows up twice, if it's in
three lists, shows up three times, etc... the result is that I can tell if the game is in a current user's list, but the catalog display screws up by showing the same game twice (since it was found twice in gameslists)
http://i82.photobucket.com/albums/j277/melhusseini/Capture2.png
How can I fix this?
EDIT: The primary display criteria for the query should be determined by the catalog parameters (genre, release, date, etc...) not if the game is already in a member's list or not... the purpose of that part is to display an add/remove button based on that status
In your outer join condition put the memberid of the specific member of interest.
LEFT OUTER JOIN gameslists ON
games.id = gameslists.gameid and gameslists.memberid = 1
You can then test gameslists.memberid if it is null then the game was not in their list.
e.g.
case when gameslists.memberid is null then 0 else 1 end as InMembersList