[英]MYSQL Get id from Table1 and Insert to Table2 while INSERTING Values (USER Form)
I have 2 tables:我有 2 张桌子:
Table1表格1
u_id email
1 user1@y.com
2 user2@h.com
and Table2和表2
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当用户从表单输入时,必须使用 u_id 将值插入 table2 示例:我需要在该用户登录到 table2 时添加值 table1 u_id,即 table1 中的 id 增量和 table1 中的 u_id 以及用户从 table2 输入时的值
i tried something this query however not successful我尝试了这个查询但没有成功
insert into table2 (u_id,values)
values ((select u_id from table1), 'values')
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.您的查询的问题是子查询返回多行(
table1
中的每行一个值),而数据库需要一个标量值。
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.我希望您知道当前用户的
email
,并希望恢复相应的u_id
以在table2
中insert
新行以及文字值。 If so, I would recommend the insert... select
syntax:如果是这样,我会推荐
insert... select
语法:
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).旁注:
values
是一个 SQL 关键字,因此不是列名的好选择(每次使用时都需要引用此标识符)。 I renamed this column to val
in the query.我在查询中将此列重命名为
val
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.