Mysql
 sql >> Datenbank >  >> RDS >> Mysql

MySQL-Join-Tabelle

Versuchen Sie diese Abfrage, aber wenn es einige Daten gäbe, könnte ich sie überprüfen, obwohl ich mit Dummy-Daten überprüft habe

select
  t.token_id,
  IFNULL(g.game,'') as Game,
  IFNULL(p.name,'') as Prize,
  case when g.game != '' then 'Assigned' when p.name != '' then 'Assigned' else 'Not assigned yet' end as `Status`
from token as t
  left join (select *
         from games
         where token_id not in(select
                     token_id
                   from prize)) as g
    on g.token_id = t.token_id
  left join (select *
         from prize
         where token_id not in(select
                     token_id
                   from games)) as p
    on p.token_id = t.token_id

EDITED
Dann sollte es am einfachsten sein

select *
from `user`
  left join token
    on user.user_id = token.user_id
  left join games
    on games.token_id = token.token_id
  left join prize
    on prize.token_id = token.token_id
where user.user_id = 1