-- Simple SQL to update the tool_count field in the categories table -- Based on log observations, we know the following columns exist: -- tools.category_id -- news.category_id -- blog_posts.primary_category_id -- Update category counts with the total count of instances UPDATE categories c SET tool_count = ( -- Count tools directly related to this category via category_id SELECT COUNT(*) FROM tools WHERE category_id = c.id ) + ( -- Count news with this category via category_id SELECT COUNT(*) FROM news WHERE category_id = c.id ) + ( -- Count blog posts with this category as primary_category_id SELECT COUNT(*) FROM blog_posts WHERE primary_category_id = c.id ); -- Verify the results (show top 20 by count) SELECT id, name, tool_count, (SELECT COUNT(*) FROM tools WHERE category_id = id) AS tools_count, (SELECT COUNT(*) FROM news WHERE category_id = id) AS news_count, (SELECT COUNT(*) FROM blog_posts WHERE primary_category_id = id) AS blog_posts_count FROM categories ORDER BY tool_count DESC LIMIT 20;