SQL如何显示当前不在团队中的玩家

huangapple go评论83阅读模式
英文:

SQL how to show players not currently in a team

问题

SELECT DISTINCT Player_tbl.PID, Player_tbl.PlayerID
FROM Player_tbl
WHERE UID = 2 AND PID NOT IN (SELECT PlayerID FROM PlayerTeam_tbl WHERE TeamID = 14)
英文:

I am trying to show players in a drop down view which is not already added to the chosen team.

When I put a = sign in my SQL-question I get all the players added to the team, but when I change it to <> it instead shows all players not in the current team but all other teams.

My problem is that I can't make a SQL-question that shows all players (PID, PlayerID) you (UID, TeamUID, PlayerUID) have created that is not currently in the choosen team (TID, TeamID).

SELECT DISTINCT Player_tbl.* 
FROM PlayerTeam_tbl 
left JOIN Player_tbl ON Player_tbl.PID = PlayerTeam_tbl.PlayerID 
WHERE TeamID &lt;&gt; 14 AND UID = 2

Player_tbl

PID Fornamn Number Coach UID
15 Stephen 85 2
16 Axel 47 nej 2
17 Kaspar 32 nej 2
18 Mats 66 nej 2
19 Oskar 99 nej 2

Team_tbl

TID Division Team Strength TemUID
14 u20 KFUM... bankvarmare 2
17 u17 Malmo mangd 2
20 u14 KFUM... give and go 2
21 u17 Frovi FC Maximus 2
23 u14 Rovani... 100 2
24 u20 Matss... programering 3
25 u14 test testar 2
26 u17 kalle k... iq 2
27 u20 London vet inte 5

PlayerTeam_tbl

PTID PlayerID TeamID TeamUID
5 15 15 2
11 15 17 2
16 15 23 2
60 18 17 2
63 17 17 2
75 19 14 2
77 16 14 2
80 17 14 2

Tables

答案1

得分: 0

不确定你使用的是哪种数据库,假设是 MS SQL 的话,查询语句会类似于:

SELECT * FROM PlayerTbl 
WHERE PID NOT IN 
(SELECT PlayerID FROM PlayerTeam WHERE TEAMUID = @teamid)
英文:

Not sure which database you're on, assuming it is MS SQL the query would look something like this:

SELECT * FROM PlayerTbl 
WHERE PID NOT IN 
(SELECT PlayerID from PlayerTeam where TEAMUID = @teamid)

答案2

得分: 0

为了获得您想要的结果:只有球员15和18应该显示,因为球员17既在14号球队又在17号球队中

如果您的数据库引擎支持NOT IN子查询,您可以简单地使用NOT IN子查询来获得结果。

这是查询

SELECT Player_tbl.pid, fornamn,number,coach,uid
FROM Player_tbl
WHERE pid NOT IN (
    SELECT
        PlayerID
    FROM
        PlayerTeam_tbl
    WHERE
        TeamID = 14
)
pid fornamn number coach uid
15 Stephen 85 null 2
18 Mats 66 nej 2

如果您的数据库引擎不支持NOT IN子查询,您可以将player_tbl放在连接的左侧,并使用GROUP BY操作符和HAVING子句来过滤任何在14号球队中的球员。

这是查询

SELECT Player_tbl.pid, fornamn,number,coach,uid
FROM Player_tbl 
LEFT JOIN PlayerTeam_tbl ON Player_tbl.PID = PlayerTeam_tbl.PlayerID 
WHERE UID = 2
GROUP BY Player_tbl.pid, fornamn,number,coach,uid
HAVING SUM(CASE WHEN TeamID= 14 THEN 1 ELSE 0 END) = 0
pid fornamn number coach uid
15 Stephen 85 null 2
18 Mats 66 nej 2
英文:

To get the result you want: only player 15 and 18 should be shown as play 17 are in both team 14 and team 17)

If your DB Engine support NOT IN subquery, you can simply get ther result with NOT IN subquery.

Here is the query:

SELECT Player_tbl.pid, fornamn,number,coach,uid
FROM Player_tbl
where pid NOT in (
    SELECT
        PlayerID
    FROM
        PlayerTeam_tbl
    WHERE
        TeamID = 14
)
pid fornamn number coach uid
15 Stephen 85 null 2
18 Mats 66 nej 2

If your DB engine dones't support NOT In sub query, you can put player_tbl in the left side of the join, and use group by operator with having clause to filter any player in team 14.

Here is the query:

SELECT Player_tbl.pid, fornamn,number,coach,uid
FROM Player_tbl 
left JOIN PlayerTeam_tbl ON Player_tbl.PID = PlayerTeam_tbl.PlayerID 
WHERE UID = 2
GROUP BY Player_tbl.pid, fornamn,number,coach,uid
HAVING sum(case when TeamID= 14 then 1 else 0 end) = 0
pid fornamn number coach uid
15 Stephen 85 null 2
18 Mats 66 nej 2

答案3

得分: 0

答案很简单,使用左连接。然而,为了澄清,如果你依赖于这个,你会得到所有不在团队里的人。或者,你是指你想要一个所有不在任何团队的球员的列表。一个人怎么可能同时属于两支活跃的球队呢。

SELECT 
    P.* 
FROM 
    PlayerTbl P
    LEFT JOIN PlayerTeam PT
    ON P.PlayerID = PT.PlayerID
WHERE
    PT.PID IS NULL

这里的关键在于 WHERE 子句,即球员不属于任何团队,不论你对团队1、2、14、27或其他任何团队有没有兴趣。如果他们不在任何团队,就可以被分配到任何团队。

英文:

The simple answer would be a left-join. HOWEVER, to clarify, if you rely on that, you would get everyone NOT on the team. -- OR-- Do you mean you want a list of all players who are not on ANY team. How could one person be associated with two active teams.

SELECT 
      P.* 
   FROM 
      PlayerTbl P
         LEFT JOIN PlayerTeam PT
            on P.PlayerID = PT.PlayerID
   WHERE
      PT.PID IS NULL

The point here with the WHERE clause is that the player is not on ANY team, regardless of your interest in team 1, 2, 14, 27, whatever. If they are not on a team, they can be assigned to ANY team.

huangapple
  • 本文由 发表于 2023年3月7日 19:44:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定