tags:

views:

313

answers:

3

Hi, I have an insert sql statement, e.g.

INSERT INTO table_a (col_a, col_b, col_c, col_d) VALUES (1, 2, 3, 4), (2, 1, 6, 9), (3, 1, 4, 5)

I want to insert this into another table, however the table I want to insert it into has a different structure to that of the sql statement (it has fewer fields) e.g.

table_b has columns 'col_a', 'col_b', 'col_d'

What do I have to do to the original sql statment so that I can get it to insert it into table_b. I guess it would be something along the lines of just ignoring the value which is in col_c and just sending this to a temp variable rather than a field.e.g.

INSERT INTO table_b (col_a, col_b, @temp_var, col_d) VALUES (1, 2, 3, 4), (2, 1, 6, 9), (3, 1, 4, 5)

Thanks

+3  A: 

How about you remove it?

INSERT INTO table_b (col_a, col_b, col_d) VALUES (1, 2, 4), (2, 1, 9), (3, 1, 5)
Leo Jweda
The insert into sql statement I have has over 100 rows to insert so don't want to go through and manually remove the rogue values. I also don't have access to the original table so i cant remove it from the export either
John
@John: Could you write a script to modify the SQL and remove the values you don't want to insert?
Mark Byers
I could but I just thought there might be an easy way where I could just change the column name to a variable name and mysql would then simply insert the value into this temp variable rather than the database.If not I will just have to write something to handle this or manually input the data myself :(
John
I don't see why you don't want to do it manually, you should be done in less than 15 minutes.Also, you don't need access to the table, you just need to modify the query wherever it is.
Leo Jweda
Use a regexp to change your SQL query:(\([^,]*\), *\([^,]*\), *\([^,]*\), *\([^,]*\))(\1, \2, \4)
vincebowdren
+1  A: 

This is ugly, and I have just tried in in SQLite, but I can image that it also works in MySQL (the documentation doesn't say it's not allowed) (update: see John's comment, it does not work in MySQL):

sqlite> create table t(a,b,c);
sqlite> insert into t (a,b,b,c) values (1,2,3,4);
sqlite> select * from t;
1|2|4
balpha
i'm not sure how that would work since you are inserting values into a column twice and if anything I would expect it to overrite the 2 with the 3. Thanks for your help though and I will give it a try.
John
@John: Yes, it's weird. Would be interesting though if there's any documentation that actually defines the intended behavior of this query.
balpha
just tried it and it didn't work. Error returned was:MySQL said: #1110 - Column 'type_id' specified twice
John
@John Okay, then it's SQLite specific.
balpha
+2  A: 

Use a temporary table:

CREATE TEMPORARY TABLE myTemp (
col_a integer,
col_b integer,
col_c integer,
col_d integer
);
INSERT INTO myTemp (col_a, col_b, col_c, col_d) VALUES (1, 2, 3, 4), (2, 1, 6, 9), (3, 1, 4, 5);
INSERT INTO table_a (SELECT col_a,col_b,col_d FROM myTemp);

The table gets dropped once your session ends (or you can remove it manually)

Andrew Dyster
This is a really smart solution, I like it!
Leo Jweda
Yeah this worked for me. Thanks for your help.
John