[Solved] MYSQL query for selection and Grouping


If I understand correctly, you basically need SUM() of all the Get_val values where either ID_One or ID_Two is 44. Afterwards, you want to display all the unique combinations of ID_One and ID_Two with the “overall sum”.

We can get the “overall sum” in a Derived Table; and then CROSS JOIN it back to the Main table, to get the required rows:

SELECT 
  DISTINCT t.ID_One, t.ID_Two, dt.tot_sum 
FROM getList AS t 
CROSS JOIN 
(
  SELECT SUM(Get_val) AS tot_sum 
  FROM getList 
  WHERE ID_One="44" OR ID_Two = '44'
) AS dt 
WHERE t.ID_One="44" OR t.ID_Two = '44'

Result

| ID_One | ID_Two | tot_sum |
| ------ | ------ | ------- |
| 44     | 50     | 3       |
| 50     | 44     | 3       |

View on DB Fiddle

4

solved MYSQL query for selection and Grouping