将多行数据合并到同一行和不同的列中:mysql

时间:2019-02-07 04:25:12

标签: mysql sql

假设您具有以下基本表:

student_id  student_name  course_name subscribed 
    001       vishnu        english      YES
    001       vishnu        arabic       NO
    001       vishnu        chinese      YES

我需要以下格式的文件:

student_id student_name     english   arabic  chinese
       001     vishnu         YES       NO     YES     

我需要mysql查询。请帮助或指导我正确的方向。

4 个答案:

答案 0 :(得分:1)

您可以尝试使用条件聚合

this.barChart = new Chart(this.barCanvas.nativeElement, {
  type: 'bar',
  data: {
    labels: this.monthcostlabor,
    datasets: this.monthcostlabor.map((elem,i) => ({ 
         label: elem, 
         backgroundColor: backgroundColor[(i % backgroundColor.length)], 
         borderColor: borderColor[(i % borderColor.length)], 
         borderWidth: 1, 
         data:[this.monthcostglobal] })); // the second push() can be included here?    
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }]
    }
  }
});

/*
  for(let bard = 0; bard < this.monthcostlabor.length; bard++){
  this.barChart.data.datasets.push({ label: this.monthcostlabor[bard], backgroundColor: backgroundColor[bard], borderColor: borderColor[bard], borderWidth: 1 });
}
this.barChart.data.datasets.forEach(element => {
  element.data.push(this.monthcostglobal)
});
this.barChart.update(); */

答案 1 :(得分:0)

您必须在分组依据下使用案例。

select
student_id,
student_name,
max(case when course_name='english' then subscribed else 'A' end) as english,
max(case when course_name='arabic' then subscribed else 'A' end) as arabic,
max(case when course_name='chinese' then subscribed else 'A' end) as chinese
from table group by student_id, student_name;

答案 2 :(得分:0)

我想指出的是:(SQL Server解决方案)

create table #temp (studentId int, studentName nvarchar(75), course_name nvarchar(75),subscribed nvarchar(3))


insert into #temp values (1,'James','English','Yes')
insert into #temp values (2,'Victor','Arabic','No')
insert into #temp values (2,'Victor','Chinese','Yes')
insert into #temp values (2,'William','Chinese','Yes')

select * from (select * from #temp) t1 pivot(max(subscribed) for course_name in ([English],[Arabic],[Chinese]))t1

studentId   studentName English Arabic  Chinese
    1         James      Yes    NULL    NULL
    2         Victor     NULL   No      Yes
    2         William    NULL   NULL    Yes

答案 3 :(得分:0)

应该是这样,只有在没有重复数据的情况下此请求才有效,例如仅针对一个学生的英语课程重复。

SELECT 
  std.student_id,
  std.student_name,
  (SELECT 
     (CASE WHEN t1.cours_name = 'english' THEN "YES" ELSE "NO" END) 
   FROM table as t1 
   WHERE 
      t1.student_id = std.student_id 
      AND 
      t1.cours_name = 'english') AS english,
  (SELECT 
     (CASE WHEN t1.cours_name = 'arabic' THEN "YES" ELSE "NO" END) 
   FROM table as t1 
   WHERE 
      t1.student_id = std.student_id 
      AND 
      t1.cours_name = 'arabic') AS arabic,
  (SELECT 
     (CASE WHEN t1.cours_name = 'chinese' THEN "YES" ELSE "NO" END) 
   FROM table as t1 
   WHERE 
      t1.student_id = std.student_id 
      AND
      t1.cours_name = 'chinese') AS chinese
FROM 
  table as std
GROUP BY std.student_id;