How to select the columns for INNER JOIN table depending on values of the joined arrays

前端 未结 2 1095
半阙折子戏
半阙折子戏 2021-01-21 18:22

I created a mySQL database with phpMyAdmin in my local server. In this database I store the names and the favourite NBA teams of my friends.This is obviously a many-to-many rela

2条回答
  •  终归单人心
    2021-01-21 19:19

    SELECT 
       CONCAT( 
        "{"
       ,     '"id"' , ":" , '"' , friends.id , '"' , ","
       ,     '"name"' , ":" , '"' , friends.name , '"' , ","
        , 
       CASE 
       WHEN relations.status = 'Current' 
         THEN CONCAT('"CurrentTeam":["',    teams.name ,'"]')
       ELSE CONCAT('"pastTeam": '   ,   '[' ,   GROUP_CONCAT( '"',teams.name, '"'),']'  )
         END   
       , "}"
       )
      AS json
    FROM 
     friends 
    INNER JOIN 
     relations 
    ON 
     friends.id = relations.friends_id
    INNER JOIN
     teams 
    ON
    relations.teams_id = teams.id
     group by friends.id,relations.status
    

    http://sqlfiddle.com/#!9/694bc69/23

提交回复
热议问题