I have three tables:
players
------
id|name
teams
-------
id|name
teams_players
-------------
id|teamID|playerID
I now want to get every entry from table "players" where players.id is for example in the team with id 15. With other words: I want to get every player that belongs to a specific team (e.g teamID=15)
I tried a join but it fails.
Here is what i got so far:
"SELECT players.*
FROM players
JOIN teams_players
ON teams_players.teamID = 15
GROUP BY players.id";
You are way overcomplicating this.
select players.*
from players
join teams_players
on players.id = teams_players.playerid
where teams_players.teamid = 15
The JOIN part has nothing to do with the number 15. The 15 is how you filter the results.
you can join your table with more than one conditions also you haven't mention the relation between your tables in the on clause
SELECT players.*
FROM players
JOIN teams_players
ON (players.id = teams_players.playerid AND teams_players.teamID = 15 )
GROUP BY players.id
Related
my heads hurt from trying to figure this out!
I have three tables
-Players
-Teams
-Games
Three junction tables with two columns.
-player_teams
-teams_games
-player_games
I need to list all Players who are in a Game (eg: Game_id = 111 from variable) that are not assigned a Team(in this game). i call them orphaned players
Basically get Teams that are in the game, get their Players and reverse match against Games_players. or the other way round i suppose.
i tried for two day no luck!
thanks!
/J
p.s after i posted this i got this far but it seems to complex!
SELECT * from players
JOIN
(SELECT DISTINCT games_players.player_id from games_players
Left JOIN
(Select team_players.player_id p1 from team_players
inner join (Select * from games_teams where games_teams.game_id = :P1) AS tm1 ON team_players.team_id = tm1.team_id) As f1
On games_players.player_id = f1.p1
where p1 is null) as q1
on players.player_id = q1.player_id
As the game is given, you can just look at the players in the game and the players of the teams in the game:
select *
from players
where player_id in
(
select player_id
from player_games
where game_id = 111
)
and player_id not in
(
select pt.player_id
from player_teams pt
join teams_games tg using (team_id)
where tg.game_id = 111
);
Does anyone know the solution to this problem ?
There are 3 Tables: orders, order_groups and stores.
I want to list the orders, with the names of the stores where the order was placed, and where the order is going to be delivered.
I keep the from_store_id, and to_store_id in the order_groups table
Listing these orders would be simple, i just left join the order_groups to orders, and select the name, from_shop_id and to_shop_id, but the problem is i want the name of the stores not the id, and the store names are placed in a different table (stores)
Here is what im talking about:
Table orders
id group_id name madeup_id
1 11 johnny cash 1
2 12 billy bob 1
LEFT JOIN order_groups on order_groups.id = orders.group_id
Table order_groups
id from_store_id to_store_id
11 55 56
12 56 55
Table stores
id store_name
55 thisstore
56 thatstore
The result im looking for is:
name from_store to_store
1.johhny cash thisstore, thatstore
2.billy bob thatstore, thisstore
The statement i have yet:
SELECT
orders.name, something as from_store, something as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
somehow join stores on the order_groups.from_store_id = stores.id
WHERE orders.madeup_id = 1
Any idea how to select and join the store names to the query ?
One more question. I actually want to list two kind of orders in one query from different tables too, im on the right track with this structure ?
SELECT a,b FROM a LEFT JOIN b ON b.something=a.something WHERE something
UNION ALL
SELECT a,b FROM c LEFT JOIN c ON c.something=a.something WHERE something
You only need to join 2 times the same table!
SELECT
orders.name, fromStore.store_name as from_store, toStore.store_name as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
left join stores fromStore on the order_groups.from_store_id = fromStore.id
left join stores toStore on the order_groups.to_store_id = toStore.id
WHERE orders.madeup_id = 1
I am struggling with the WHERE part of a query. The query itself contains a LEFT JOIN based on an ID that is present in both tables. However I require the where statement to only return the largest single result that is present in one of the columns. Currently I am return all the values in the join, including values that I do not want.
My Current SQL is
SELECT u.uid, t.id
GROUP_CONCAT(u.forename, ' ', u.surname) AS name,
GROUP CONCAT(DISTINCT scores.points) AS point
FROM users AS U
JOIN teamname AS t
LEFT JOIN (
SELECT team_id, id
FROM games AS g
LEFT JOIN (
SELECT points, team_id
FROM scores as s
) AS S ON t.id = S.team_id
WHERE IF (S.points > 3, S.points > 2, S.point =1)
) AS G ON t.id = G.team_id
ORDER BY surname ASC;
The result of such might be something along the lines of
NAME | TEAM | GAMES | POINTS
Joe | 1 | 1,2,3,4 | 1,3,3,2,3
In this instance the first game was a draw and was replied resulting in a higher points score, I am only wanting the higher points score based on that game.
Any help would be appreciated.
Updated with Tables
users
uid
forename
surname
Team
id
teamname
uid
games
id
team_id
points
Still not quite sure if I understood your tables correctly. It seems a users has one or more teams, each team has one or more games with one or more results per game. You want to show for each user and each team the games concatenated in one column and the highest points for each game concatenated in a second column.
If my assumptions are correct the following query should do the trick. Basically, you first group the data by user/team/game and select the max points per game, then you group the results by user/team and concatenate the games and points.
Please let me know if I misunderstood any of your requirements.
Example in an SQL Fiddle
SELECT
t.uid,
t.forename,
t.team_id,
GROUP_CONCAT(t.game_id) as games,
GROUP_CONCAT(t.max_points) as max_points
FROM (
SELECT
users.uid,
users.forename,
teams.id AS team_id,
games.id AS game_id,
max(games.points) as max_points
FROM
users
LEFT JOIN teams ON users.uid = teams.uid
LEFT JOIN games ON teams.id = games.team_id
GROUP BY
users.uid,
users.forename,
teams.id,
games.id
) t
GROUP BY
t.uid,
t.forename,
t.team_id
I have three tables, I'll just list the important columns
db_players
id | name
players
id | teamid | careerid
db_teams
The db_teams id links to the players teamid.
I need to run a query where I select rows from db_players as long as db_teams.id isn't in a row in players as teamid where the careerid = 1.
I've never attempted this type of query with mysql before, I know I could do two queries and involve php but I'm intrigued as to whether it's possible with a pure db query.
Thanks.
EDIT - simpler now.
SELECT dp.first_name
FROM tbl_foot_career_db_players dp
INNER JOIN tbl_foot_career_players p
ON p.playerid != dp.id
WHERE p.careerid = 1
The idea is that I want to return all rows from tbl_foot_career_db_players WHERE the id from that table isn't present in a row in tbl_foot_career_players in the column playerid. And the tbl_foot_career_players.careerid must also equal 1.
List all db_players that are not in players with career = 1
SELECT d.*
FROM db_players d
LEFT JOIN players p
ON p.player_id = d.id
AND p.career = 1
WHERE p.id IS NULL
Try to JOIN them with db_players id is not in players teamid.
SELECT db_players.* FROM db_players
LEFT JOIN players ON players.id != db_players.id
LEFT JOIN db_teams ON players.teamid = db_teams.id
WHERE careerid = 1
I have three tables: Players, PlayersArchive, Races. The Players and PlayersArchive tables are exactly the same structure.
In Players and PlayersArchive we have PlayerID and Name.
In Races we have in one row:
RaceID, Record1PlayerID, Record2PlayerID, Record3PlayerID, Record4PlayerID and Record5PlayerID
The task is to SELECT the whole Races table, but instead of the player ID fields it should return their names.
e.g.:
Races table:
RaceID: 1
Record1PlayerID: 2
Record2PlayerID: 1
Record3PlayerID: 0
Record4PlayerID: 0
Record5PlayerID: 0
Players table:
PlayerID: 1
Name: Jhon
PlayersArchive table:
PlayerID: 2
Name: Jack
result:
1 Jack Jhon NULL NULL NULL
You have two problems with your data structure. The first is that you should have a separate table for the players in each race, with one row per player/race combination. Putting them in separate columns on a single row is a bad idea.
The second problem is putting the names in two different tables. From your question, I cannot tell if both the players table and playersarchive table are necessary. So, I'll assume the archive is not necessary.
The solution is to join from the races table to the table with the players information:
select r.*,
p1.name as name1,
p2.name as name2,
p3.name as name3,
p4.name as name4,
p5.name as name5
from races r left outer join
players p1
on r.record1playerid = p1.playerid left outer join
players p2
on r.record1playerid = p2.playerid left outer join
players p3
on r.record1playerid = p3.playerid left outer join
players p4
on r.record1playerid = p4.playerid left outer join
players p5
on r.record1playerid = p5.playerid left outer join
If the data is really in both Players and PlayersArchive, then players might need to be:
(select distinct playerid, name from ((select p.* from players) union all (select pa.* from playersarchive)) t) p1 . . .
This combines the two tables together for looking up the name.
This will only demonstrate a posible solution to what you ask.
Select RaceId, p1.Name AS Name1 From Races AS r
Left Join (
Select * From Players
Union Select * From PlayersArchive
) AS p1 On r.Record1PlayerID = p1.PlayerID;
You should repeat this left join 4 more times whithin your query to get all 5 names.
Please note that with big data this might take a long time to execute.