tags:

views:

78

answers:

3

TABLES:

  • student(name,points)
  • playsin(name->student(name), score)

I'm trying to use:

UPDATE STUDENT
   SET points = points + CONSTANT * (SELECT score 
                                       FROM PLAYSIN 
                                      WHERE playsin.name = student.name);
+2  A: 

Try:

UPDATE STUDENT s
   SET points = s.points + CONSTANT * (SELECT p.score 
                                        FROM PLAYSIN p 
                                       WHERE p.name = s.name)

Table aliases are a very good habit to have.

OMG Ponies
Can't try it out right now, but is the alias really required? It seems that the references are clear even without the aliases.
IronGoofy
@IronGoofy: I can't test either :/ But it's either the fact of referencing the points value in the equation to update it, or the join to the PLAYSIN table I figure.
OMG Ponies
Thanks, using aliases solved my problem. Thank you very much.
Link
+1  A: 

You might also try updating an in-line view:

UPDATE (select s.name student_name,
               s.points student_points,
               p.score  playsin_score
          from STUDENT s,
               PLAYSIN p
         where p.name = s.name)
SET    student_points = student_points + CONSTANT * playsin_score;

It also limits the rows in STUDENT that are updated to only the set for which there is a row in PLAYSIN (you might also consider in your current code what happens to SCORE if that subquery could return NULL). You'd need a unique or primary key on playsin.name to use this syntax to avoid a join cardinality check error, but if this is not feasible then a MERGE statement might be useful. A MERGE might be worth considering anyway if you also have code to add into STUDENT any new names in PLAYSIN.

David Aldridge
A: 

You can try next:

UPDATE student s, playsin p SET s.points = s.points + CONSTANT * p.score WHERE p.name=s.name

Victor Vostrikov