Home > Database > Mysql Tutorial > MySQL中ROLLUP的替代方法

MySQL中ROLLUP的替代方法

WBOY
Release: 2016-06-07 15:19:56
Original
1474 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 今天从MySQL中提取数据,使用ROLLUP统计后,想在数据左边列中加入项目名称,使用SQL Sql代码 SELECT (CASE WHEN ISNULL(PLAYERNO) THEN PLAYERNO=TOTAL ELSE PLAYERNO END) AS PLAYERNO,SUM(AMO

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  今天从MySQL中提取数据,使用ROLLUP统计后,想在数据左边列中加入项目名称,使用SQL

  Sql代码

  SELECT (CASE WHEN ISNULL(PLAYERNO) THEN PLAYERNO=TOTAL ELSE PLAYERNO END) AS PLAYERNO,SUM(AMOUNT) FROM penalties

  GROUP BY PLAYERNO WITH ROLLUP

  结果是:

  这样

  +----------+-------------+

  | PLAYERNO | SUM(AMOUNT) |

  +----------+-------------+

  |        6   |      100.00 |

  |        8   |        25.00 |

  |       27  |      175.00 |

  |       44  |      130.00 |

  |      104 |        50.00 |

  |     NULL|      480.00 |

  +----------+-------------+

  6 rows in set, 1 warning (0.00 sec)

  显示为空值,修改如下:

  Sql代码

  SELECT PLAYERNO,SUM(AMOUNT) FROM penalties

  GROUP BY PLAYERNO

  UNION

  SELECT TOTAL,SUM(AMOUNT)

  FROM penalties

  显示结果:

  +----------+-------------+

  | PLAYERNO | SUM(AMOUNT) |

  +----------+-------------+

  | 6          |      100.00 |

  | 8          |        25.00 |

  | 27        |      175.00 |

  | 44        |      130.00 |

  | 104      |        50.00 |

  | TOTAL  |      480.00 |

  +----------+-------------+

  6 rows in set (0.00 sec)

MySQL中ROLLUP的替代方法

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template