I have two tables, one is Players and second one is Participant's List. I keep full data about player only in Players table and i want to display Participant's data but i don't want to show ID, which are stored in this table. I want to replace them with data relevant to stored ID. So i wrote script:
SELECT pl.surname as Name FROM players pl, participants_list p
WHERE pl.player_id = p.player_id
And it works, but it replaces only one value which is the name of player. I need to replace also name of the tournament from other table. I don't know how to combine those two operations, which may result in getting 2 columns replaced with value. For example
Smith | Tournament 1
Kowalsky | Tournament 2
The data I have is:
Players Table
Player's id|Player's Name| and more
Events Table
Event's id|Event's Name| and more
Participant's Table
Player's id|Event's id
Try this out:
select b.player_name,c.event_name
from
Participants_table a
left join
players table b
on a.player_id = b.player_id
left join
events_table c
on a.event_id = c.event_id;
Let me know in case of any queries.
Related
SQL noob here. I have a database of soccer matches that I am trying to learn/practice SQL with.
In one table (called "Match") there is the match_api_id, date, home_team_api_id, away_team_api_id, home_team_goal, away_team_goal.
In another table (called "Team") there is the team_api_id, team_long_name.
Right now I am trying to do a query to show the ID of the match, the date, the home team's name, and the away team's name.
SELECT M.match_api_id, M.date, T.team_long_name, T.team_long_name
FROM Match M
JOIN Team T ON (M.home_team_api_id = T.team_api_id)
JOIN Team T ON (M.away_team_api_id = T.team_api_id)
LIMIT 10
This code worked when I only used one join (the first one) to show the home team's name. However, when I add the second join it gives me the ambiguous column name error. How do I alter this code so that I can also display the away team's name?
YOu need different table aliases. Otherwise T is ambiguous:
SELECT M.match_api_id, M.date, TH.team_long_name, TA.team_long_name
FROM Match M JOIN
Team TH
ON M.home_team_api_id = TH.team_api_id JOIN
Team TA
ON M.away_team_api_id = TA.team_api_id;
LIMIT 10
I am trying to create a SQL SELECT that displays the player's username and the name of the team associated with the player. This is my code but it don't works like expected:
SELECT Player.userName AS Player,
Teams.TeamName AS [Team Name]
FROM Players, Teams
INNER JOIN Players
ON Team.ID = Player.userName
This is the Team's table
This is the player's table I just included the names only.
This is the full Player's Table with the contents of what is inside the table.
Your syntax is a bit off in your FROM clause. The best rule of thumb here is to NEVER use a comma in your FROM clause (the only exception is if you want to join EVERY row from one table to EVERY row of another creating a cartesian product of the two tables, but we rarely do that).
When you specify the relationship of the two tables in the ON clause of your JOIN you need to put the column(s) from each table that they have in common. A Team.ID will NEVER match to a Players.userName, so that is not the proper join condition.
Assuming you have a TeamID column in your players table so you know which Team each Player is on, you will have SQL that will look like:
SELECT Players.userName AS Player,
Teams.TeamName AS [Team Name]
FROM Teams
INNER JOIN Players
ON Team.ID = Players.[TeamName_]
I am working on an online chess game.
In the database, I have created a user table, and a table for the games.
I used a participant table to connect them. This way I could create many games for each user, and have 2 players for each game.
As you can see:
The query I need: get a user Id and return all the games he/she played. Result table will contains Date, moves, players (the row players will show the username of each user that played in this game).
For example if I have row in userTable:
Id:1 UserName:p1
After I use the query it will return:
Date | Players | moves
11/11/11 p1vsp2 ...
11/12/12 p1vsp3 ...
11/10/10 p1vsp2 ...
Though I dont encourage getting code from Stack Overflow here is a start to help you get going.
You have linked all your tables in the above diagram. If you are having troubles with the joins here is a start
SELECT .... (insert field names here) FROM User U
Next we add the JOINS
JOIN Participant p on u.id = p.id
JOIN GAME g on g.idgame = p.id_game
Now you will have to put in some criteria to further filter it using a WHERE clause
WHERE someField = someValue
I have two tables:
The first one (players) contains player records with it ID's and names.
Another one (results) contains results:
ID of 1 player against ID of 2 player
How can I select their names?
SELECT *
FROM results
ORDER by result_date DESC
It looks like this for now. I want to have instead of numbers real player names.
You just need to set two different alias for the players table:
select *
from results
join players as pair1players on results.Pair1 = pair1players.ID
join players as pair2players on results.Pair2 = pair2players.ID
order by result_date desc
Here, supposing Pair1 is the name of the field "Pair #1" you showed in the screenshot, and Pair2 is the name of the field "Pair #2".
This could help:
SELECT players.ID, players.Name
FROM players
INNER JOIN results ON players.ID=results.playersID
I have 3 data tables.
In the entries data table I have entries with ID (entryId as primary key).
I have another table called EntryUsersRatings in there are multiple entries that have entryId field and a rating value (from 1 to 5).
(ratings are stored multiple times for one entryId).
Columns: ratingId (primary key), entryId, rating (integer value).
In the third data table I have translations of entries in the first table (with entryId, languageId and title - translation).
What I would like to do is select all entries from first data table with their titles (by language ID).
On a top of that I want average rating of each entry (which can be stored multiple times) that is stored in EntryUsersRatings.
I have tried this:
SELECT entries.entryId, EntryTranslations.title, AVG(EntryUsersRatings.rating) AS AverageRating
FROM entries
LEFT OUTER JOIN
EntryTranslations ON entries.entryId = EntryTranslations.entryId AND EntryTranslations.languageId = 1
LEFT OUTER JOIN
EntryUsersRatings ON entries.entryId = EntryUsersRatings.entryId
WHERE entries.isDraft=0
GROUP BY title, entries.entryId
isDraft is just something that means that entries are not stored with all information needed (just incomplete data - irrelevant for our case here).
Any help would be greatly appreciated.
EDIT: my solution gives me null values for rating.
Edit1: this query is working perfectly OK, I was looking into wrong database.
We also came to another solution, which gives us the same result (I hope someone will find this useful):
SELECT entries.entryId, COALESCE(x.EntryUsersRatings, 0) as averageRating
FROM entries
LEFT JOIN(
SELECT rr.entryId, AVG(rating) AS entryRating
FROM EntryUsersRatings rr
GROUP BY rr.entryId) x ON x.entryId = entries.entryId
#CyberHawk: as you are using left outer join with entries, your result will give all records from left table and matching record with your join condition from right table. but for unmatching records it will give you a null value .
check out following link for the deta:
http://msdn.microsoft.com/en-us/library/ms187518(v=sql.105).aspx