How to sum a field of a certain ID with multiple records?

2

Thestorygoeslikethis:apoliticiancanbevotedonforseveral%of%,sothereasonforthesame%candidate%containmultiplerecordswithinthetable.

ThezonasbeingID,hehaving6recordsinthedatabasereferringtothe6differentzoneshereceivedvotes.

HowtosetupaID_CANDIDATOthatwillshow120000000171registrationwithalltheinformationofthiscandidateandtotal

IDID_CANDIDATOTOT_VOTOS211200000001713394512000000017140573120000000171513881200000001713809212000000017175296120000000171439

AvisualexampleofwhatI'mlookingforis on this page . The last field is sql .

    
asked by anonymous 06.03.2015 / 21:24

2 answers

3

You can use SUM . To do this, you need to group the records:

SELECT SUM(tot_votos) as total_votos FROM ... GROUP BY id_candidato

To get the candidate information in the same poll, you will need a JOIN with the candidate table:

SELECT SUM(tz.tot_votos) as total_votos, tc.*
    FROM tabela_zonas tz
    JOIN tabela_candidatos tc ON tc.id = tz.id_candidato
    WHERE ...
    GROUP BY tz.id_candidato
    
06.03.2015 / 21:50
2

You can use SUM() to add the votes and GROUP BY to join the records by your candidate's code.

SELECT SUM(TOT_VOTOS) FROM sua_tabela
WHERE id_candadito = $seu_id GROUP BY id;

You can retrieve information such as name and the like by adding JOIN . But at first%% will give you the result you want. But if it is not the expected result or you need to modify or add something, please let us know!

    
06.03.2015 / 21:50