mysql join table

后端 未结 1 1101
长发绾君心
长发绾君心 2021-01-20 15:48

I have 4 tables.

  1. Table A that has users - (user_id, username, and name)
  2. Table B that has tokens - (token_id, user_id). Ea
1条回答
  •  盖世英雄少女心
    2021-01-20 16:26

    Try this query but if there was some data i would be able to check although i have checked with dummy data

    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
    Then it should be the most simple thing to do

    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
    

    0 讨论(0)
提交回复
热议问题