sql query for chess game - sql

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

Related

How to select ALL results from a SQL query and eliminate the null values

Imagine I have four tables:
Agents
| agent_id | agent_name |
Teams
| team_id | team_name |agent_id |
Menu
| menu_id | menu_name |
Team_assignment
| menu_id | team_id|
I need to write a query that selects all agents that are assigned to all teams and all queues and disregard the ones that are not assigned to a queue. Note that every agent is always assigned to a team but it's not necessary that the agent is assigned to a queue.
Since you stated that this is for a school project, I'll try to stay within the guidelines mentioned here: How do I ask and answer homework questions?
From what I can make up from your question you basically want to select all the data from the different tables joining them on one of the columns in the first table a equals = a column from the second table b. Most commonly where the primary key from one table equals the foreign key from another table. Then you want to add conditions to your query where for example some column from table 1 equals = some value.
Do you catch my drift? 😏
No?
You want to SELECT a.*, b.* everything FROM table Agents a JOINing table Teams b ON column a.agent_id being equal to = column b.agent_id
You probably want to JOIN another table, lets say Team_assignment c ON column c.team_id being equal to = b.team_id.
You can JOIN more tables in the same way.
Sadly, I do not understand what you mean by the ones that are not assigned to a queue but it sounds like a condition that your query needs to match, so WHERE the potential column a.is_assigned_to_queue equals = true AND for example a.agent_name IS NOT NULL
If you got this far you should have been able to catch onto my drift 😎, congrats. This way hopefully you also got a better understanding of how building query works, instead of me just blatantly giving you the answer and you learn nothing from it. Like this:
SELECT a.*, b.*, c.*, d.* FROM Agents a
JOIN Teams b ON a.agent_id = b.agent_id
JOIN Team_assignment c ON c.team_id = b.team_id
JOIN Menu d ON d.menu_id = c.menu_id
WHERE a.is_assigned_to_queue = true
AND a.agent_name IS NOT NULL;
Now it is possible copy and pasting the snippet above will not work, that is because I'm not an SQL expert and I had to refresh my old memories about SQL myself by googling it. But that's the nice part of actually learning it. Being able to explain it to someone else :)

substituting ID for value

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.

Filtering out records in SQL with a join

I am creating an app that makes guest lists for greek life events at universities.
The two tables I am working with are 'student' table and 'participant' table.
The fields in the student table are: student_id, student_name, university, and chapter.
Students with chapter id's are considered members, and students without chapter id's are considered guests when making guest lists(participant table).
The participant table fields are: participant_id, member(which is related to student_id), guest (which is also related to student_id), and event.
When trying to add guests to the guest list for an event, I wrote the following sql query to filter out students from different universities and that aren't in chapters and weren't already on the list:
$student = getColumn("SELECT guest FROM participant WHERE event = '$event'");
$university = getSqlValue("SELECT university FROM student WHERE student_id = '$member'");
$f->setOption('filter',
"SELECT student_name
FROM `student`
LEFT JOIN participant ON student.student_id = participant.guest
WHERE student.chapter = ''
AND student.university = '$university'
AND participant.guest != '$student'");
So, I know this isn't going to work, because I have a whole array for $student, but even if I try it with one student id, the query doesn't work. It returns empty. If I remove the last AND particpant.guest!= $student, then the query returns all the students at the university that are not members of a chapter.
My question has two parts:
Why wouldn't that query work with one value for student?
Can someone think of a better way to go about doing this?

Query SQL Database for Home and Away team based on Team ID

I'm working on setting up a Football (Soccer) betting database using data from Betfair. This is my first ever Database and I'm working in MS SQL Server 2012.
I have set up 4 tables: Match, Team, Competition & Data.
My Match table contains columns: MatchID(PK), CompetitionID(FK), DateKickOff, TimeKickOff, TeamIDHome(FK), TeamIDAway(FK), ScoreHome, ScoreAway
My Team table contains columns: TeamID(PK), TeamName
I have been trying out some queries at this stage so that I can have a think about whether my tables are Normalised correctly. I wanted to run a query that would return 4 columns: Home Team Name, Home Score, Away Score, Away Team Name.
If I run this I get the first 3 columns back:
SELECT TeamName TeamH, ScoreHome, ScoreAway
FROM Match AS t
INNER JOIN Team AS n
ON t.TeamIDHome = n.TeamID
If I run this I get my fourth column:
SELECT TeamName TeamA
FROM Match AS t
INNER JOIN Team AS n
ON t.TeamIDAway = n.TeamID
But I cannot Union the 2 functions as I get an error: All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
I believe that the error is caused because it tries to add the Away Team Names to the same column as the home team names. Can anybody suggest how I can query this correctly or let me know if I have a bad database design? Thanks
You don't need two queries for this. Add another join to Team linking the Match.TeamIdAway with team.TeamId. Like:
select HomeTeam.TeamName as TeamH, AwayTeam.Teamname as TeamA, M.ScoreHome, M.ScoreAway
from Match as M
join Team as HomeTeam on M.TeamIdHome = HomeTeam.TeamId
join Team as AwayTeam on M.TeamIdAway = AwayTeam.TeamId

Return all entries in groups where a group member satisfies condition x

In Ms Access, I'm trying to write a SQL query that answers a question like:
Return all player IDs on all teams in a list of football players where at least one member of the team is injured.
I'm new to SQL. I've tried something like
SELECT pid FROM players WHERE team_id IN
(SELECT team_id FROM players WHERE injury = 'yes')
Access won't accept this IN. Is there a simple way to do this? I'd rather run thus as one query, instead of creating separate queries, so I can change it easily as necessary.
I find it hard to believe that it doesn't support IN, nevertheless you can do it using a join:
SELECT distinct players.pid
FROM players as injured
INNER JOIN players on players.team_id = injured.team_id
WHERE injured.injury = 'yes'
I used distinct in case there's multiple injured players on the one team, which would result in the players from that team being returned multiple times