Reham Fahmy
Reham Fahmy

Reputation: 5063

Operate updates on MYSQL

let say i've database with millions of lines and i would like to create php batch file that apply certain changes on all certain lines.

Example : say we have millions of such database lines (id-cat-subcat)

INSERT INTO `drink` VALUES (1, 'Non-Alcoholic', 'tea');
INSERT INTO `drink` VALUES (2, 'Non-Alcoholic', 'tea');
INSERT INTO `drink` VALUES (3, 'Non-Alcoholic', 'coffee');
INSERT INTO `drink` VALUES (4, 'Non-Alcoholic', 'pepsi');

so i want php command that says

for only cat='Non-Alcoholic' and that have subcat='tea' do change of cat from cat='Non-Alcoholic' to be cat='tea'

so final results will be

INSERT INTO `drink` VALUES (1, 'tea', 'tea'); 
INSERT INTO `drink` VALUES (2, 'tea', 'tea'); 
INSERT INTO `drink` VALUES (3,'Non-Alcoholic', 'coffee'); 
INSERT INTO `drink` VALUES (4,'Non-Alcoholic', 'pepsi');

thank you for help

Upvotes: 2

Views: 37

Answers (1)

Mat
Mat

Reputation: 206689

The following SQL query will do the replacement you want:

UPDATE `drink`
  SET cat = 'tea'
  WHERE cat    = 'Non-Alcoholic'
    AND subcat = 'tea'

Just wrap it up with whichever MySQL binding you're using in PHP.

Upvotes: 1

Related Questions