Home > database >  Parameters of a variable number of mysql insert statement how to write
Parameters of a variable number of mysql insert statement how to write

Time:09-20

Now is such, for example, I have a table row 10 fields, and then for the first time I can in the ten column data in order, then can the table structure may be changed in another place, may I know after changing a few column data, if still inserted in order, how to write a dynamic SQL statement to achieve insert process,

CodePudding user response:


Then could the table structure may be in another place change

You mean, table structure can dynamically change? Just is 10 columns, is likely to be eight columns? Why is there such a demand, feel not very reasonable,,,

CodePudding user response:


The best is to put the field into a column, such as:

Id field name value

CodePudding user response:

Using the mybatis foreach

CodePudding user response:

The parameter design for a table, the table with two columns,

First as a self growth Numbers (starting from 1), 1 on behalf of the general meaning of the first parameter, 2, 3, on behalf of the second parameter, 3 represents the third parameter...

The second column represents the value of the parameter

So, how many lines represent received this table contains many parameters

CodePudding user response:

I threw a brick here, hope you can bring jade,
According to the following steps can be, is an example, should be able to meet your requirements,

First of all, build table script,

The CREATE TABLE ` table1 ` (

` KEY ` VARCHAR (2) NOT NULL DEFAULT ',

` VALUE ` VARCHAR (3) the NOT NULL DEFAULT '

);

Then, insert the test data, you can on this basis, add, modify, delete data,

Insert into ` table1 ` (` KEY `, ` VALUE `) values

(' 1 ', 'blue'),

(' 1 ', 'red'),

(' 2 ', 'large'),

(' 2 ', 'small'),

(' 61 ', 'boutique'),

(' 61 ', 'common goods');

Finally, execute the following code, it is not a a separate execution, but, after all chose to perform together,

The SET @ R1:=0;

SET @ : R2=0;

The SET @ S:=';

SELECT CONCAT (" SELECT @ : R2=@ R2 + 1 S1, CONCAT_WS ('/', ", S1, S2 ') FROM ', S2, 'WHERE', S3, '; ') INTO @ S FROM (SELECT GROUP_CONCAT (' T ', R, '. ', 'VALUE') S1, GROUP_CONCAT (' TABLE1 ', 'T', R) S2, REPLACE (GROUP_CONCAT (' T ', R, 'KEY=', T.K EY), ', ', 'AND') S3 FROM (SELECT @ R1:=@ R1 + 1 R, T.K EY FROM TABLE1 T GROUP BY T.K EY) T) T;

PREPARE stmt1 FROM @ S;

The EXECUTE stmt1;

  • Related