tags:

views:

61

answers:

2

Hy at all, today is the day of ...question. I've a single table, with a relation master-detail like this:

RecordID    MasterID   Field1 Field2 .... NrDetail
1           0          xxx     yyyy       1
2           0          aaaa     bbbb      2
3           1          hhhhh   ssss       0
4           2          eee     sssss      0
5           2          jjj      hhhh      0

As you can see, NrDetail contain the total of "child record". Unfortunately, i've to create this field... and i would like to write it in my table. So my SQL question is: how to do this type of SQL to write the field NrDetail ?

Something like:

UPDATE table SET NrDetail= 
  (SELECT COUNT(*) as Total FROM table WHERE MasterID= RecordID) 

But i think there's some mistake...

Thank you in advance !

+3  A: 

I think that you have forgetten to specify which MasterID you want to compare with which RecordID. How about:

UPDATE table t1 SET NrDetail= 
  (SELECT COUNT(*) as Total FROM table t2 WHERE t1.MasterID=t2.RecordID) 
Christian
I get this mysql error: Error 1093: you can't specify target table 't1' for update in FROM clause ...!
stighy
you will need to replace t1 and t2 with the names of your tables
espais
yes i know ... i write this: UPDATE mytable t1 SET TotalChild= (SELECT COUNT(*) as Total FROM mytable t2 WHERE t1.id = t2.masterid) but it not work!
stighy
+1  A: 
UPDATE table
SET NrDetail =  (
                Select Count(*)
                FROM table t2 
                Where t2.RecordID = table.MasterID
                )

In an update statement, when you want to reference the table being updated, you need to use the full reference for columns(tablename.columnname, or ideally schema.tablename.columnname). If you are using the same table in a subquery, you need to alias the table in the subquery but again use the full reference for the outer table.

ADDITION Since you mentioned that you are using MySql, you could try something like so:

Update post
    Join    (
            Select p1.idpadre, Count(*) Total
            From post p1
            Group By p1.idpadre
            ) Z
        On Z.idpadre = post.idpost
Set post.NrDetail = Z.Total
Thomas
UPDATE post t1SET NrRisposte = ( Select Count(*) as total FROM post t2 Where t2.idpadre = t1.idpost ) I get: you can't specify t1 for update in from clause
stighy
@stighy - The problem is `UPDATE post t1`. You can't alias in the UPDATE clause. Instead you need `UPDATE post Set NrRisposte = (Select Count(*) From post As T2 Where T2.idpadre = post.idpost )`
Thomas
@thomas: i'm going crazy ! ;) I'm using MySQL and i get again the error: "1093: You can't specify target table 'post' for update in FROM clause'
stighy
@stighy - It sounds like you are running into anomalies with MySQL's parser. BTW, you should add the mysql tag to your question. You might try encapsulating the counts into a subquery which you use in a join. I've amended my post to illustrate.
Thomas
W O N D E R F U L It works ! Now i'm trying to understand the syntax. Thank you THOMAS !
stighy