sql >> Database teknologi >  >> RDS >> Mysql

Reference til ydre forespørgsel i underforespørgsel JOIN

SELECT p1.*, p2.*
FROM player p1
JOIN team t1
  ON t1.id = p1.team_id AND t1.id = My_Team_ID
LEFT JOIN player p2
  ON p2.id != p1.id
JOIN team t2
  ON t2.id = p2.team_id AND t2.id != My_Team_ID
LEFT JOIN game g1
  ON (g1.playerA_id = p1.id OR g1.playerB_id = p1.id)
  AND (g1.playerA_id = p2.id OR g1.playerB_id = p2.id)
WHERE g1.id IS NULL

Hvis jeg bruger 1 til My_Team_ID, får jeg følgende resultater, som viser de resterende matches:

id  team_id name            id  team_id     name
1   1       Laurent Dupuis  6   2           Alec Russell
2   1       Stéphane Leroy  6   2           Alec Russell
3   1       Julien le Guen  4   2           Mark Johnsson
3   1       Julien le Guen  6   2           Alec Russell



  1. Krypter data i SQLite

  2. MySQL/MariaDB TRIGGER

  3. Android Studio markerer/fremhæver ikke Kotlin Room DAO-forespørgsler, når strengen optager mere end 1 række

  4. YEARWEEK() Eksempler – MySQL