加入两个表列

时间:2016-02-09 13:00:38

标签: php mysql

我有两个表格如下所示:

In [37]: df1 = pd.DataFrame([['USA',1],['USA',2],['USA',3],['FRA',1],['FRA',2]], columns = ['country', 'value'])

In [38]: df2 = pd.DataFrame([['USA',10],['FRA',20]], columns = ['country', 'value'])

In [39]: df1 = df1.set_index('country')

In [40]: df2 = df2.set_index('country')

In [41]: mask = df1['value'] >= 2

In [42]: idx = df1.index[mask]

In [43]: idx = idx.unique()

In [44]: df1
Out[44]:
         value
country
USA          1
USA          2
USA          3
FRA          1
FRA          2

In [45]: df2
Out[45]:
         value
country
USA         10
FRA         20

In [46]: idx
Out[46]: array(['USA', 'FRA'], dtype=object)

In [47]: df1.update(df2.loc[idx])

In [48]: df1
Out[48]:
         value
country
USA         10
USA         10
USA         10
FRA         20
FRA         20

team_members

teamid   team_name

  1      Rockstars
  2      Gangsters

我想得到这样的结果:

teamid    team_member_name
  1         Rob
  1         Mike
  2         John
  2         Paul

我试过这个:( 无效

teamid      team_members    team_name

   1        Rob, Mike       Rockstars
   2        John, Paul      Gangsters

请帮忙

1 个答案:

答案 0 :(得分:3)

您需要的是GROUP_CONCAT

SELECT t1.teamid, t1.team_name, GROUP_CONCAT(t2.team_member_name)
FROM teams AS t1
INNER JOIN team_members AS t2 ON t1.teamid = t2.teamid
GROUP BY t1.teamid, t1.team_name

Demo here