如何将多行数据显示成一行mysql

时间:2017-02-14 14:24:02

标签: mysql sql pivot-table

嗨,伙计们,我是新手学习MySQL,我有这张桌子

#define ERROR 10
#define MAIN "Main:"

#define LOG(lvl,mod,fmt,...) \
    char msg[256] = {0}; \
    snprintf(msg, 256, "%s: %d: "fmt,mod,lvl,##__VA_ARGS__)

int main()
{   ....
    LOG(ERROR, MAIN, "This is a log statement.\n"); // Doesn't compile
    LOG(10, "Main:", "This is a log statement.\n"); // Compiles
    ....
}

现在我想要这种表格的表格 表2

error: expected `)' before ‘;’ token
error: expected primary-expression before ‘,’ token
error: expected `;' before ‘)’ token

1 个答案:

答案 0 :(得分:0)

您可以使用聚合来执行透视和用户变量来生成序列号。

set @fid := 0;

select @fid := @fid + 1 fid,
    c_file,
    e.name,
    operation,
    sum(case when c_no = 1 then qty end) c_no1,
    sum(case when c_no = 2 then qty end) c_no2,
    sum(case when c_no = 3 then qty end) c_no3,
    sum(case when c_no = 4 then qty end) c_no4,
    sum(case when c_no = 5 then qty end) c_no5,
    sum(qty) total,
    rate,
    sum(qty) * rate total_times_rate
from t
group by c_file,
    e.name,
    operation,
    rate
order by min(f_id);