如何在单列显示多个以逗号分隔的值?

时间:2015-03-15 08:29:16

标签: mysql inner-join

我正在使用mysql来执行查询。有以下7个表。

  • 约会//非原子的,例如。值 - > 1,7,3
  • 性别//原子价值
  • 模块//非原子的,例如。值 - > 12,33
  • 程序
  • 人员
  • 学生

我试过'concat','find_in_set'& 'in'功能但无法使其工作。我怎样才能显示多个值@ relations'comvention'& '模块'?

以下陈述是我能得到的最接近的陈述。如果需要更多详细信息,请告知我们,谢谢。

SELECT sta.staName
      , r.rank
      , sta.appointmentID
      , a.appointment
      , m.moduleCode 
  FROM staff AS sta 
 JOIN rank AS r 
    ON (sta.rankID = r.rankID) 
 JOIN appointment AS a 
ON (sta.appointmentID = a.appointmentID) 
 JOIN module AS m 
    ON (sta.teachModuleID = m.moduleID) 
  WHERE sta.genderID = 1;

1 个答案:

答案 0 :(得分:1)

SELECT sta.staName, r.rank, sta.appointmentID, 
       group_concat(distinct a.appointment) as appointments,
       group_concat(distinct m.moduleCode) as moduleCodes
FROM staff AS sta 
INNER JOIN rank AS r ON sta.rankID = r.rankID
INNER JOIN appointment AS a ON find_in_set(sta.appointmentID, a.appointmentID) > 0
INNER JOIN module AS m ON find_in_set(sta.teachModuleID, m.moduleID) > 0
WHERE sta.genderID = 1
GROUP BY sta.staName, r.rank, sta.appointmentID