简体   繁体   English

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

[英]Combine multiple rows data into same row and different columns : mysql

Suppose you have following base table :假设您有以下基表:

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

I need it in the following format :我需要以下格式:

student_id  student_name     english   arabic  chinese
------------------------------------------------------
    001     vishnu             YES       NO     YES     

I need the mysql query for this.为此我需要 mysql 查询。 Please help or guide me in the right direction.请帮助或指导我朝着正确的方向前进。

You can try using conditional aggregation您可以尝试使用条件聚合

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

You have to use case with group by.您必须将 case 与 group by 一起使用。

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;

I would pivot that: (SQL Server Solution)我会认为:(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

It should be like this, this request work only if there's no duplicate data, like duplicate english course just for one student.应该是这样的,这个请求只有在没有重复数据的情况下才有效,比如只为一个学生开设重复的英语课程。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM