Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2003, 07:12 PM
Totta Totta is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 3 Totta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old December 6th, 2003, 11:22 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h
Reputation Power: 37
Code:
UPDATE USERS set numpart=(select count(*) from PARTICIPATION WHERE fk_userid=pk_userid) WHERE pk_userid in (select fk_userid from participation)


Quote:
this doesn't work


That type of comment is pretty useless.

Be more elaborate. If there is an error, post the message. Otherwise, describe what is not working.

Reply With Quote
  #3  
Old December 6th, 2003, 04:33 PM
Totta Totta is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 3 Totta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old December 6th, 2003, 05:49 PM
shammat shammat is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2003
Location: Munich, Bavaria
Posts: 993 shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level)shammat User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 44 sec
Reputation Power: 66
This works (at least with Postgres):
Code:
UPDATE USERS set numpart=(select count(*) from PARTICIPATION WHERE fk_userid= users.pk_userid); 

Reply With Quote
  #5  
Old December 6th, 2003, 06:19 PM
Totta Totta is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 3 Totta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > Help with update-query


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway