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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?