《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
zhuzixian520
最佳答案

楼上的解答很赞,你的这个需要应该是需要用到条件查询 case when then else end…

SELECT
    pid,
    uid,
    SUM( score ) AS score,
    MAX( created_at ) AS created_at,
    SUM( CASE WHEN type = 1 THEN score ELSE 0 END ) AS type1,
    SUM( CASE WHEN type = 2 THEN score ELSE 0 END ) AS type2,
    SUM( CASE WHEN type = 3 THEN score ELSE 0 END ) AS type3,
    SUM( CASE WHEN type = 4 THEN score ELSE 0 END ) AS type4
FROM
    demo_x
GROUP BY
    uid

亲测有效

Laravel

3年前 评论
讨论数量: 3
zhuzixian520

楼上的解答很赞,你的这个需要应该是需要用到条件查询 case when then else end…

SELECT
    pid,
    uid,
    SUM( score ) AS score,
    MAX( created_at ) AS created_at,
    SUM( CASE WHEN type = 1 THEN score ELSE 0 END ) AS type1,
    SUM( CASE WHEN type = 2 THEN score ELSE 0 END ) AS type2,
    SUM( CASE WHEN type = 3 THEN score ELSE 0 END ) AS type3,
    SUM( CASE WHEN type = 4 THEN score ELSE 0 END ) AS type4
FROM
    demo_x
GROUP BY
    uid

亲测有效

Laravel

3年前 评论

用 sql 麻烦的吧。 用 php 逻辑组合要方便得多吧

3年前 评论
SELECT
    MAX( uid ) uid,
    MAX( pid ) pid,
    SUM( score ) score,
    SUM( CASE WHEN type = 1 THEN score ELSE 0 END ) type1,
    SUM( CASE WHEN type = 2 THEN score ELSE 0 END ) type2,
    SUM( CASE WHEN type = 3 THEN score ELSE 0 END ) type3,
    MAX( created_at ) created_at 
FROM
    table 
GROUP BY
    uid

看一下是否符合你的要求。

3年前 评论
zhuzixian520

楼上的解答很赞,你的这个需要应该是需要用到条件查询 case when then else end…

SELECT
    pid,
    uid,
    SUM( score ) AS score,
    MAX( created_at ) AS created_at,
    SUM( CASE WHEN type = 1 THEN score ELSE 0 END ) AS type1,
    SUM( CASE WHEN type = 2 THEN score ELSE 0 END ) AS type2,
    SUM( CASE WHEN type = 3 THEN score ELSE 0 END ) AS type3,
    SUM( CASE WHEN type = 4 THEN score ELSE 0 END ) AS type4
FROM
    demo_x
GROUP BY
    uid

亲测有效

Laravel

3年前 评论