Branchwell

AI Resource Pro / website / scripts / minimal-update-category-counts.sql

1-- MINIMAL SQL to update the tool_count field in the categories table
2-- Using only confirmed columns to avoid any errors
3
4-- Update the tool_count in the categories table
5UPDATE categories c
6SET tool_count = (
7 -- Count tools with this category
8 SELECT COUNT(*) FROM tools
9 WHERE category_id = c.id
10) + (
11 -- Count news with this category
12 SELECT COUNT(*) FROM news
13 WHERE category_id = c.id
14);
15
16-- Output the results for verification
17SELECT
18 c.id,
19 c.name,
20 c.tool_count,
21 (SELECT COUNT(*) FROM tools WHERE category_id = c.id) AS tools_count,
22 (SELECT COUNT(*) FROM news WHERE category_id = c.id) AS news_count
23FROM
24 categories c
25ORDER BY
26 c.tool_count DESC;