简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM