user6146087
user6146087

Reputation:

MYSQL Insert id from another table

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?

Upvotes: 5

Views: 26593

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269843

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';

Upvotes: 1

zxcvc
zxcvc

Reputation: 355

Try this

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

Miss out brackets

Hope it helps

Upvotes: 19

Related Questions