Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Pivot的MySql分组_Mysql_Sql_Group By_Pivot - Fatal编程技术网

使用Pivot的MySql分组

使用Pivot的MySql分组,mysql,sql,group-by,pivot,Mysql,Sql,Group By,Pivot,我在mysql中有一个表(比如studentinfo) 我希望以下输出不使用任何额外或临时表。(可能使用“分组依据”和“透视”) 您需要创建一个透视表,下面是一个例子,其中有很多很好的例子 基本上,你只是想做这样的事 SELECT Rollnumber, property CASE WHEN Property="Name" then Property end as Name, CASE WHEN Property="Year" then Proper

我在mysql中有一个表(比如studentinfo)

我希望以下输出不使用任何额外或临时表。(可能使用“分组依据”和“透视”)


您需要创建一个透视表,下面是一个例子,其中有很多很好的例子

基本上,你只是想做这样的事

 SELECT Rollnumber, 
        property
      CASE WHEN Property="Name" then Property end as Name,
      CASE WHEN Property="Year" then Property end as Year,
      CASE WHEN Property="City" then Property end as City,
 FROM whatever_table
 GROUP BY Rollnumber

您可以根据自己的需要对其进行修改,以获得所需的结果。

这应该满足您的要求:

select si.Rollnumber, 
       max(case when si.Property = 'Name' then si.Value end) as Name,
       max(case when si.Property = 'Year' then si.Value end) as Year,
       max(case when si.Property = 'City' then si.Value end) as City
from StudentInfo si
group by si.Rollnumber;
请尝试以下内容:

      select * from (SELECT A.Rollnumber,if(A.Property='Name',Value,
        (select B.Value from StudentInfo B where B.Property='Name' and B.Rollnumber=A.Rollnumber LIMIT 1)) as Name,
        if(Property='Year',Value,
        (select B.Value from StudentInfo B where B.Property='Year' and B.Rollnumber=A.Rollnumber LIMIT 1)) as Year,
       if(Property='City',Value,
        (select B.Value from StudentInfo B where B.Property='City' and B.Rollnumber=A.Rollnumber LIMIT 1)) 
         as City 
         FROM StudentInfo A) Temp group by Rollnumber

你曾尝试过什么来达到你想要的结果?用你的尝试编辑你的问题。我混淆了如何将分组和枢轴组合在一起,而我自己就停留在那里了。
      select * from (SELECT A.Rollnumber,if(A.Property='Name',Value,
        (select B.Value from StudentInfo B where B.Property='Name' and B.Rollnumber=A.Rollnumber LIMIT 1)) as Name,
        if(Property='Year',Value,
        (select B.Value from StudentInfo B where B.Property='Year' and B.Rollnumber=A.Rollnumber LIMIT 1)) as Year,
       if(Property='City',Value,
        (select B.Value from StudentInfo B where B.Property='City' and B.Rollnumber=A.Rollnumber LIMIT 1)) 
         as City 
         FROM StudentInfo A) Temp group by Rollnumber
SELECT
    name.rollnumber,
    name.value AS name,
    year.value AS year,
    city.value AS city
FROM 
    StudentInfo name
    INNER JOIN StudentInfo year ON name.rollnumber = year.rollnumber
    INNER JOIN StudentInfo city ON name.rollnumber = city.rollnumber    
WHERE
    name.property = 'name' AND
    year.property = 'year' AND
    city.property = 'city' AND