Fakipo
Fakipo

Reputation: 190

How to insert values into a table according to the index values of columns in MySQL database?

I want to add values according to index in my table.

This is the code I am trying right now.

insert into A(1,2) values ('ABC','CBA');

Upvotes: 0

Views: 1772

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1270503

The only way that you can add columns according to position is to leave out the column list and include all columns:

insert into A
     values ('ABC', 'CBA');

That said, you really should be explicit about which columns are getting values, by including the column names:

insert into A (col1, col2)
     values ('ABC', 'CBA');

Or using the MySQL set extension:

insert into A (col1, col2)
     set col1 = 'ABC', col2 = 'CBA';

Upvotes: 0

GMB
GMB

Reputation: 222582

The insert statement does not accepts column indexes, but column names. Say your table called a has three columns called id, col1 and col2, and you want to insert into the last two columns, you would do:

insert into a (col1, col2) values('ABC', 'CBA');

Upvotes: 1

Related Questions