NEW
#1

Zero2Cool
Elite Member
Joined: Oct 14, 2006
Posts: 44,952

Zero2Cool
Elite Member
Joined:Oct 14, 2006
Posts:44,952
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.
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]
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.
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]
0
SlickVision, Methodikal, Kevin and 5 others
NEW
#2

Zero2Cool
Elite Member
Joined: Oct 14, 2006
Posts: 44,952

Zero2Cool
Elite Member
Joined:Oct 14, 2006
Posts:44,952
[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.
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.
0
SlickVision, Methodikal, Kevin and 5 others
NEW
#3

Zero2Cool
Elite Member
Joined: Oct 14, 2006
Posts: 44,952

Zero2Cool
Elite Member
Joined:Oct 14, 2006
Posts:44,952
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.
0
SlickVision, Methodikal, Kevin and 5 others
NEW
#4

Zero2Cool
Elite Member
Joined: Oct 14, 2006
Posts: 44,952

Zero2Cool
Elite Member
Joined:Oct 14, 2006
Posts:44,952
[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.
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.
0
SlickVision, Methodikal, Kevin and 5 others
NEW
#5

Zero2Cool
Elite Member
Joined: Oct 14, 2006
Posts: 44,952

Zero2Cool
Elite Member
Joined:Oct 14, 2006
Posts:44,952
+1
0
SlickVision, Methodikal, Kevin and 5 others