|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with update-query
I really don't know how to write this so I'm asking you.
I have 2 tables (USERS and PARTICIPATION). All I want to do is count the number of occurencies in PARTICIPATION for each user in USERS and update a numeric field in USERS with this value. Example: The table USERS has 2 fields (pk_userid, numpart) The table PARTICIPATION has at least 1 field (fk_userid) There can be infinite numbers of data in PARTICIPATION and I need to populate the field numpart for each user with the number of occurencies for THAT user in PARTICIPATION. And I want to do it with only one SQL-command. Something like: UPDATE USERS set numpart=(select count(*) from PARTICIPATION WHERE fk_userid=pk_userid) WHERE pk_userid=fk_userid but of course this doesn't work. Please help. |
|
#2
|
|||
|
|||
|
Code:
UPDATE USERS set numpart=(select count(*) from PARTICIPATION WHERE fk_userid=pk_userid) WHERE pk_userid in (select fk_userid from participation) Quote:
That type of comment is pretty useless. Be more elaborate. If there is an error, post the message. Otherwise, describe what is not working. |
|
#3
|
|||
|
|||
|
Well, if you would have read my complete post you would see that I don't have a clue on how to write the query I'm asking for.
The SQL included in my post is just rubbish and was only included to show you the CONCEPT. It was not intended to work and it won't work... neither logically nor in syntax. I could have written "sdlkjfhlskdfjlksdf" with the same result. So disregard the SQL in my first post and instead please provide me with an example on how to write the query I'm asking for. /Totta |
|
#4
|
|||
|
|||
|
This works (at least with Postgres):
Code:
UPDATE USERS set numpart=(select count(*) from PARTICIPATION WHERE fk_userid= users.pk_userid); |
|
#5
|
|||
|
|||
|
Yes this was exactly what I was looking for!
Quite easy now when I see it but a big thanks to you anyway. Best regards /Totta |
![]() |
| Viewing: Dev Shed Forums > Databases > Database Management > Help with update-query |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|