I have 2 tables:
Table1
u_id email 1 user1@y.com 2 user2@h.com
and Table2
id u_id values
had to insert values to table2 with u_id when user enters from form Example:I need to add the value table1 u_id while that user is logged in into table2 that is id increments in table and u_id from table1 and values as user enters from table2
i tried something this query however not successful
insert into table2 (u_id,values) values ((select u_id from table1), 'values')
Advertisement
Answer
The problem with your query is that the subquery returns multiple rows (one value per row in table1
), while the database expects a scalar value.
I would expect that you know the email
of the current user, and want to recover the corresponding u_id
to insert
a new row in table2
, along with a literal value. If so, I would recommend the insert ... select
syntax:
insert into table2 (u_id, val) select u_id, 'foo' from table1 where email = 'user1@y.com'
Side note: values
is a SQL keyword, hence not a good choice for a column name (you would need to quote this identifier every time you use it). I renamed this column to val
in the query.