dongta1824 2015-02-26 09:56
浏览 23
已采纳

将重复行转换为新列MySql

Basically I have a MySql table

--------    -----------   --------
 cv_id       all_stages    amount
--------    -----------   --------
  12           S1           5
  13           S4           2
  13           S2           1  
  14           S1           3
  14           S3           2

I have to write a query so that, based oncv_id, I need to have a single entry. And I need to generate few more columns.

What I want is

--------    ---------  --------  --------  --------   -------
 cv_id       stage1     stage2    stage3    stage4     total
--------    ---------  --------  --------  --------   -------
  12           S1                                       5
-------------------------------------------------------------
  13                      S2                  S4        3
-------------------------------------------------------------
  14           S1                   S3                  5
-------------------------------------------------------------

How should I arrange all stages to the respective stage columns based on cv_id and totaling the amount column?

P.S - I need to execute query to generate new columns

  • 写回答

1条回答 默认 最新

  • douhu1990 2015-02-26 10:02
    关注

    This is not a good practice to generate columns dynamically. Columns represents common properties for a model, and you will end up with a table that has 100 columns and only one column is used per row. What you are trying to do, can be achieved with another table that links cv_id with stages.

    For example

    table cv_stage
    Columns: cv_id, stage_name
    

    You can then always query cv_id's that have certain stages or any other information you may need.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题