0

I need to insert a table with some values (eg: 'NDA' in this case). This seems to work well if I have just one value to be inserted. I have around a dozen of similar values, is there a was i can tweak this query to insert say { 'NDA', 'SDM', 'APM' } values. Was curious to know if it can be done without a stored procedure or copy pasting the same statements over and changing the values.

INSERT IGNORE INTO customer_feature (customer_id, feature)
SELECT c.id, 'NDA' FROM
customer as c
where c.edition = 'FREE_TRIAL';

Reference: mysql -> insert into tbl (select from another table) and some default values

Community
  • 1
  • 1
Chinmay
  • 731
  • 1
  • 8
  • 19
  • What is the condition which will produce the values? – geoand Apr 25 '14 at 21:27
  • I am not following you. Any column in MySQL can have a default value specified. If you don't specify a value for that column on insert, then that record will get the default value for such a column. – Mike Brant Apr 25 '14 at 21:29
  • Defaults for varchars? I am not too sure about that. But I guess it's database dependend. – Peter Apr 25 '14 at 21:31
  • Anyway: This question is just to unclear. Try to write in a way such that not only you know what you are talking about rather than that everyone knows what you mean. – Peter Apr 25 '14 at 21:33
  • After all: Sound more like conditional default values which you could place in a table, definining your condition and your default value. So you could add new defaults whithout modifying too much of your queries. – Peter Apr 25 '14 at 21:34
  • @Peter, Mike: Sorry for the confusion by using 'default'. – Chinmay Apr 25 '14 at 21:35

1 Answers1

2

Is this what you want?

INSERT IGNORE INTO customer_feature(customer_id, feature)
    select c.id, f.feature
    from customer c cross join
         (select 'NDA' as feature union all select 'SDM' union all select 'APM'
         ) f
    where c.edition = 'FREE_TRIAL';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786