Welcome to your Green Bay Packers Online Community!

Since 2006, PackersHome has been providing a unique experience for fans.
Your participation is greatly anticipated!
Login or Register.
Options
View
Go to last post Go to first unread
Offline Zero2Cool  
#1 Posted : Thursday, January 6, 2011 3:30:33 AM(UTC)
Zero2Cool

Rank: Legend

Yahoo! Fantasy Football - Gold: 2009FleaFlicker Fantasy Football - Silver: 2010Yahoo! NCAA March Madness - Silver: 2011ESPN NCAA March Madness - Bronze: 2010Yahoo! NCAA March Madness - Bronze: 2013

United States
Posts: 25,617
Joined: 10/13/2006(UTC)
Location: Green Bay, WI

Applause Given: 1,832
Applause Received: 1,978

I'd like a SQL statement that finds the amount of posts for each user, then updates the specific field with that number.



On table_fpro_posts I want the count of posts that equal the poster_id and then update the field field_user_posts on table_users.

The relation between the tables would be user_id from the table_users table and poster_id from the table_fpro_posts table.


Code:
table_users        table_fpro_posts
user_id        =    poster_id



I've noticed several members with a post count that is not accurate and I'd like to fix that, but not sure where to start with the SQL command.


Getting the count isn't too hard, but I don't want to go through each member and manually update them. That's not fun.
[php]

SELECT COUNT(post_id)
FROM `cvs_fpro_posts`
WHERE poster_id = 2[/php]

UserPostedImage
Click here and find the LATEST Packers News!
Sponsor
Offline Zero2Cool  
#2 Posted : Thursday, January 6, 2011 3:49:58 AM(UTC)
Zero2Cool

Rank: Legend

Yahoo! Fantasy Football - Gold: 2009FleaFlicker Fantasy Football - Silver: 2010Yahoo! NCAA March Madness - Silver: 2011ESPN NCAA March Madness - Bronze: 2010Yahoo! NCAA March Madness - Bronze: 2013

United States
Posts: 25,617
Joined: 10/13/2006(UTC)
Location: Green Bay, WI

Applause Given: 1,832
Applause Received: 1,978

[php]UPDATE table_users
SET user_posts = COUNT(*)
FROM table_fpro_posts
WHERE table_fpro_posts.poster_id = table_users.user_id[/php]

That was my first guess, but I haven't ran it ... scared I'll break shit.

UserPostedImage
Click here and find the LATEST Packers News!
Offline Zero2Cool  
#3 Posted : Thursday, January 6, 2011 4:34:42 AM(UTC)
Zero2Cool

Rank: Legend

Yahoo! Fantasy Football - Gold: 2009FleaFlicker Fantasy Football - Silver: 2010Yahoo! NCAA March Madness - Silver: 2011ESPN NCAA March Madness - Bronze: 2010Yahoo! NCAA March Madness - Bronze: 2013

United States
Posts: 25,617
Joined: 10/13/2006(UTC)
Location: Green Bay, WI

Applause Given: 1,832
Applause Received: 1,978

Code:
SELECT table_fpro_posts.poster_id, count(table_fpro_posts.poster_id), table_users.user_id, table_users.user_posts
FROM table_users LEFT JOIN table_fpro_posts
ON table_users.user_id = table_fpro_posts.poster_id
GROUP BY table_fpro_posts.poster_id


This displays the members post count on their profile, next to the actual post count.

UserPostedImage
Click here and find the LATEST Packers News!
Offline Zero2Cool  
#4 Posted : Thursday, January 6, 2011 4:47:16 AM(UTC)
Zero2Cool

Rank: Legend

Yahoo! Fantasy Football - Gold: 2009FleaFlicker Fantasy Football - Silver: 2010Yahoo! NCAA March Madness - Silver: 2011ESPN NCAA March Madness - Bronze: 2010Yahoo! NCAA March Madness - Bronze: 2013

United States
Posts: 25,617
Joined: 10/13/2006(UTC)
Location: Green Bay, WI

Applause Given: 1,832
Applause Received: 1,978

[php]UPDATE table_users
SET table_users.user_posts = (SELECT COUNT(table_fpro_posts.poster_id)
FROM table_fpro_posts
WHERE table_users.user_id = table_fpro_posts.poster_id)[/php]

This did the trick.

UserPostedImage
Click here and find the LATEST Packers News!
Offline Zero2Cool  
#5 Posted : Saturday, January 8, 2011 4:16:05 PM(UTC)
Zero2Cool

Rank: Legend

Yahoo! Fantasy Football - Gold: 2009FleaFlicker Fantasy Football - Silver: 2010Yahoo! NCAA March Madness - Silver: 2011ESPN NCAA March Madness - Bronze: 2010Yahoo! NCAA March Madness - Bronze: 2013

United States
Posts: 25,617
Joined: 10/13/2006(UTC)
Location: Green Bay, WI

Applause Given: 1,832
Applause Received: 1,978

+1

UserPostedImage
Click here and find the LATEST Packers News!
Rss Feed 
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Notification

Icon
Error



Recent Topics
1h / Green Bay Packers Talk / Zero2Cool

1h / Green Bay Packers Talk / Zero2Cool

10h / Random Babble / DakotaT

13h / Green Bay Packers Talk / DakotaT

16h / Green Bay Packers Talk / musccy

20h / Fantasy Sports Talk / Zero2Cool

21h / Green Bay Packers Talk / Wade

23h / Green Bay Packers Talk / Dulak

22-Jul / Green Bay Packers Talk / Mucky Tundra

22-Jul / Green Bay Packers Talk / beast

22-Jul / Random Babble / dhazer

22-Jul / Green Bay Packers Talk / Tezzy

21-Jul / Around The NFL / musccy

21-Jul / Green Bay Packers Talk / beast

21-Jul / Random Babble / wpr


Tweeter

Copyright © 2006-2014 PackersHome.com™. All Rights Reserved.