简体   繁体   中英

Merge all data of parent child relation-ship from same table in mysql

I have a one table like this

id 值 轮胎 1 2 10 1 1 5 2 3 10

I expect to create table like this this

在此处输入图像描述

*yellow color is header sum of value or tire

I try with union an join but still not valid

any suggest for this problem. thanks

Perhaps GROUP BY... WITH ROLLUP is what you want here:

SELECT id, SUM(value), SUM(tire)
FROM yourTable
GROUP BY id, value WITH ROLLUP
ORDER BY -id DESC, SUM(value) DESC;

下面演示链接的屏幕截图

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM