3

If i have a table that Example is and one coloumn in it that (for example) colour is how i can do something like;

    INSERT INTO Example VALUES ('Red','Black','Green');

instead of writing the code 3 times seperatly for each value?

ProgrammerPotato
  • 505
  • 1
  • 4
  • 11

3 Answers3

3
INSERT INTO Example (column_name) VALUES ('Red'),('Black'),('Green');
000
  • 26,951
  • 10
  • 71
  • 101
3

Yes you can do this, but your format is off.

INSERT INTO `Example` (`color`)
VALUES
('Red'),
('Black'),
('Green')

Each row must be separated by a comma with values for the row in parenthesis.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
0

Here is another way that will work with any database.

insert into example
select 'Red'
from SomeSmallTable
union
select 'Blue'
from SomeSmallTable
union
select 'Green'
from SomeSmallTable
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • what. is `SomeSmallTable` as alias of `dual`? – 000 Jun 13 '13 at 23:01
  • 1
    The `from` clauses aren't needed. From the docs: "DUAL is purely for the convenience of people who require that all SELECT statements should have FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no tables are referenced." – 000 Jun 13 '13 at 23:03
  • Dual is not available with all databases. Plus not all databases allow select queries without where clauses. – Dan Bracuk Jun 14 '13 at 01:04
  • Hi Dan, do some tests with your code and you'll see something wrong. – leonardo_assumpcao Jun 14 '13 at 02:05
  • I agree with Joe - in MySQL you don't need the `from` clause at all, so the following will work `insert into example select 'Red' union select 'Blue' union select 'Green'` – Tim Rogers Nov 03 '17 at 20:59