1

I have multiple tables, I can list their rows to my website.

What I want is where last column has same value, I want to add up rows values and list as one line.

What I have:

Hioss
AUR
Top
1
1
0
Shen

Hioss
AUR
Top
1
1
0
Shen

Kanani
AUR
Jungle
1
1
0
Reksai

I don't want to get data like this. If last column has same values (Shen) I want to sum int values and show as one line at my website.

What I want to do:

Hioss
AUR
Top
2
2
0
Shen

Kanani
AUR
Jungle
1
1
0
Reksai

My mysql query:

mysql_query("SELECT * FROM table1 UNION SELECT * FROM table2"); 

How can I do it? What should I do?

4
  • 1
    You can wrap your UNION query in an outer query that groups/sums them, ie. SELECT col1, col2, col3, SUM(col4), SUM(col5), col6 FROM (SELECT * FROM table1 UNION SELECT * FROM table2) a GROUP BY col6 (note the col1-col6 are just examples, as you don't give explicit column names) Commented Feb 14, 2016 at 22:22
  • all values are listing as 0 0 0 0 00 0 :/ Commented Feb 14, 2016 at 23:03
  • @Sean col1 col2 col3s are working but sum(colx) are not working ints are listing as 0 0 0 0 others are normal Commented Feb 14, 2016 at 23:09
  • i added "as x"s after "sum(x)"s and it worked thanks! Commented Feb 15, 2016 at 15:00

1 Answer 1

1

Try this. i dont know the fieldnames. You must change it

SELECT fieldnam1,fieldnam2, sum(fieldnam3),fieldnam4
FROM (
    SELECT * FROM table1 
    UNION 
    SELECT * FROM table2
) as result
GROUP by fieldnam4;
Sign up to request clarification or add additional context in comments.

4 Comments

mysql_query("SELECT player,takim, sum(oynananoyun),sum(win),sum(lose),sum(kill),sum(death),sum(assist),sum(cs),sum(gold),sum(goldshare),sum(gpm),sum(egpm),sum(fb),sum(dmg),sum(dmgshare),sum(dpm),sum(wardplaced),sum(wardcleared),sum(wardplacedpm),sum(wardclearedpm),sum(wardplacedshare),sum(wardclearedshare),sum(cspm),sum(dakika),sum(saniye),sum(kp),sum(deathpercentage) FROM ( SELECT * tclhafta1mac1 UNION SELECT * FROM tclhafta1mac2 ) as result GROUP by champion "); it gives: right syntax to use near 'tclhafta1mac1 UNION SELECT * FROM tclhafta1mac2 ) as result GROUP by champion' at line 2
i was missing "from" i added it but it still doesnt work doesnt give errors but all values are 0 0 0 0 0
fieldnam1,fieldnam2 working but sum(fieldname3) not working. ints are not working texts are worknig :/
i added "as x"s after "sum(x)"s and it worked thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.