Jump to content
  • 0
Sign in to follow this  
Hadeszeus

Query Question mysql

Question

I want to add new character variable to all existing char_id in char_reg_num_db using sql query.

How I am going to do that? If my key = limit_var , index = 0, and value = 1

 

THanks!

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Never done anything like this, but if you feel like experimenting try the cursors example here: http://www.databasejournal.com/features/mysql/mysql-cursors-and-loops.html

 

Of course, experiment as much as you want in a backup or a test DB :D. The query you want to use for each cursor repetition is like this:

 

INSERT INTO `char_reg_num_db` (`char_id`, `key`, `index`, `value`) VALUES (EVERY_CHAR_ID_GOES_HERE, 'limit_var', '0', '1');

 

Or you could get even smarter and do something like this (totally untested) code:

-- Copy structure of char_reg_num_db into a temporary tableCREATE TABLE IF NOT EXISTS `temp_table` (  `char_id` int(11) unsigned DEFAULT '0',  `key` varchar(32) CHARACTER SET latin1 COLLATE latin1_bin DEFAULT '',  `index` int(11) unsigned DEFAULT '0',  `value` int(11) DEFAULT '0',  PRIMARY KEY (`char_id`,`key`,`index`),  KEY `char_id` (`char_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;-- Add all existing char_ids into our tableINSERT INTO `temp_table` (`char_id`) SELECT `char_id` FROM `char`;-- Add the data we want to add for every user (row in the table)UPDATE `temp_table` SET `key` = 'limit_var', `index` = '0', `value` = '1' WHERE 1;-- Insert the temporary data into our char_reg_num_db tableINSERT INTO `char_reg_num_db` SELECT * FROM `temp_table`;-- Drop our temporary tableDROP TABLE `temp_table`;

 

 And that's it. Hope it works. You should restart the server or make all your users relog for this to take effect.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.