skip to Main Content

I’m having really hard time trying to make this work. I have this table :
evid_record

Its named evid_record and I want to sum columns : ‘Kolokvijum teorija’ + ‘Kolokvijum zadaci’ + ‘Aktivnost’ + ‘Prisustvo’ + ‘Seminarski rad’ + Domaci rad’.

And the result should be displayed in ‘Predisp. Obaveza’.

I managed to get it partially work from phpmyadmin/mysql

with this code:

SELECT id
     , SUM(teorija + zadaci + akt + pris + semrad + domrad) 
  FROM evid_record 
 GROUP 
    BY id

I get the correct results as you can see in the picture :my result

But I don’t know how should I insert that summed data into my column ‘Predisp. obaveza’. I’m not even sure if I’m on a right way. I found some similar situations people asked, tried to apply them on my project, and it didnt work..

2

Answers


  1. Consider storing data like this:

    id activity score
     1 teorija     10
     1 zadaci       5 
     1 akt          9
     1 pris        12
     1 semrad      10
     1 domrad       2  
    
    Login or Signup to reply.
  2. Keep in mind SUM is an aggregate function, it will add the values across all rows. You can simply add the columns together directly.

    You’re looking for an UPDATE statement here, as the data clearly already exists in the table and I don’t believe you’re inserting new/raw data

    UPDATE evid_record
    SET `Predisp. Obaveza` = (teorija + zadaci + akt + pris + semrad + domrad)
    #if you would like to limit the clause, but
    #since this is a table-wide function, you can omit it
    WHERE evid_record.id = :some-id
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search