AI Resource Pro / website / scripts / update-category-counts-from-logs.sql
| 1 | -- SQL to update the tool_count field in the categories table |
| 2 | -- Based on the exact column names shown in the logs |
| 3 | |
| 4 | -- From logs, we know the tables have these columns: |
| 5 | -- tools: id, name, slug, description, long_description, category_id, pricing, url, key_features, pros, cons, review, deployment, featured, created_at, updated_at |
| 6 | -- news: id, title, content, url, image_url, source, article_date, created_at, updated_at, category_id |
| 7 | -- categories: id, name, icon, tool_count, created_at, updated_at, slug, description |
| 8 | |
| 9 | -- Update the tool_count in the categories table |
| 10 | UPDATE categories c |
| 11 | SET tool_count = ( |
| 12 | -- Count tools with this category |
| 13 | SELECT COUNT(*) FROM tools |
| 14 | WHERE category_id = c.id |
| 15 | ) + ( |
| 16 | -- Count news with this category |
| 17 | SELECT COUNT(*) FROM news |
| 18 | WHERE category_id = c.id |
| 19 | ); |
| 20 | |
| 21 | -- Output the results for verification |
| 22 | SELECT |
| 23 | c.id, |
| 24 | c.name, |
| 25 | c.tool_count, |
| 26 | (SELECT COUNT(*) FROM tools WHERE category_id = c.id) AS tools_count, |
| 27 | (SELECT COUNT(*) FROM news WHERE category_id = c.id) AS news_count |
| 28 | FROM |
| 29 | categories c |
| 30 | ORDER BY |
| 31 | c.tool_count DESC; |