Create a MySQL table cyclically and update the two tables.

Source: Internet
Author: User

Create a MySQL table cyclically and update the two tables.

Create tables cyclically Using Stored Procedures

DELIMITER //

Create procedure test. create_channel_avail ()

BEGIN

DECLARE '@ I' INT (11 );

DECLARE '@ sqlstr' VARCHAR (2800 );

SET '@ I' = 1;

WHILE '@ I' <101 DO

SET @ sqlstr = CONCAT (

"Create table channel_avail ",

'@ I ',"(

'Prop' VARCHAR (40 ),

'Stay _ date' date,

'Roomcode' VARCHAR (40 ),

'Channel _ Code' VARCHAR (40 ),

'Status' VARCHAR (1) DEFAULT 'A ',

'Createtime' DATETIME comment' create timestamp ',

'Updatetime 'datetime comment' update timestamp ',

Primary key ('prop', 'stay _ date', 'roomcode', 'Channel _ Code ')

) ENGINE = INNODB

Default charset = utf8;

"

);

PREPARE stmt FROM @ sqlstr;

EXECUTE stmt;

SET '@ I' = '@ I' + 1;

End while;

END;

CALL test. create_channel_avail_db ();


Drop procedure test. create_channel_avail_db;


Note the following when using MySQL database, update, and select to update table data:

For example, table test1
A B
1
2 B
3 c

Table test2
A B
2 d
3 e
4 f
A simple combination of update and select is intended to change columns B of Rows a = 2 and a = 2 in Table test1 to columns d and e.
Update from test1 set B = (select B from a where test1.a = test2.a)
Note that this syntax has a trap. In this case, the values of a = 2 and a = 3 in the updated table test1 are indeed modified to d and e, however, because the data in Row a = 1 is not in the result set, all data is set as null, that is, except that some data in the result set is updated, no value is set to null (columns that are not allowed to be null are not tested yet)

The inner join method is the result we want, so we should replace the preceding SQL statement with this method.


Update test1 inner join test2 ON a. id = B. id

SET test1. B = test2. B

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.