Home > database > MYSQL to ID the same sum and display a few lines of data in the last column?
MYSQL to ID the same sum and display a few lines of data in the last column?
Time:09-19
For example: ID A 1 10 1 34 1 542 23 2 45 3 33
After the query: ID A, B 1 October 98 1 34 98 1, 54, 98 2 23 68 2 45 68 3, 33, 33
CodePudding user response:
Without your table, I built a temporary table, you perform the following statements can be directly,
SELECT T. *, (SELECT SUM (T1) A) FROM ( SELECT '1' ID, '10' FROM A DUAL UNION ALL SELECT '1', '34' FROM DUAL UNION ALL SELECT '1', '54' FROM DUAL UNION ALL SELECT '2', '23' FROM DUAL UNION ALL SELECT '2', '45' FROM DUAL UNION ALL SELECT '3', '33' FROM DUAL) T1 WHERE T1. ID=B T.I D) FROM ( SELECT '1' ID, '10' FROM A DUAL UNION ALL SELECT '1', '34' FROM DUAL UNION ALL SELECT '1', '54' FROM DUAL UNION ALL SELECT '2', '23' FROM DUAL UNION ALL SELECT '2', '45' FROM DUAL UNION ALL SELECT '3', '33' FROM DUAL) T;