4

i have the follow doubt

I have 2 tables:

id customers
1  alan
2  beth
3  john

and

id id_customers value
1  1            bar  
2  1            foo
3  2            baz

Example:I need to add the value 'alfa' in second table and link this to id 3 from the first.

How i do this?

2 Answers2

18

Try this

insert into tab2 (id_customers, value)
values ((select id from tab1 where customers='john'), 'alfa');

Miss out brackets

Hope it helps

zxcvc
  • 345
  • 1
  • 16
  • Work perfect for me. Thank you! I did not find any example that uses select in values –  Jun 01 '17 at 02:19
1

Wouldn't you just do an insert?

insert into t2 (id_customers, value)
    values (3, 'alfa');

This assumes that id is auto-incrementing. If not, you'll need to assign that a value as well.

Based on your comment, use insert . . . select:

insert into t2 (id_customers, value)
    select id, 'alfa'
    from t1
    where name = 'john';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • i need to search the id by customer name, because this is an varible in my environment –  Jun 01 '17 at 02:16