Sometimes we don’t want using the GUI to manage thousands of products in Magento. Instead we want do it using PHP scripts, this will automate the task and create unlimited possibilities in handling vast amount of data efficiently.

Through these sets of SQL statement, you can

  • add product to categories
  • remove product from categories
  • you can use webapi or native php shell script.

Source: Programmatically add Magento products to categories – Stack Overflow