简体   繁体   English

如何将数据插入表中,其中一个值应该是另一个表的外键?

[英]How do I insert data into a table where one of the values should be a foreign key from another table?

I wanted to update the post table (main table) with those data:我想用这些数据更新帖子表(主表):

User: "helper"
Title: "Title"
Description: "Description"

However, the post table userid column only accepts an integer (foreign keys from the user table (userid)), not username string itself.但是,post 表 userid 列只接受一个整数(来自用户表 (userid) 的外键),而不是用户名字符串本身。

How can I do this with INSERT, SELECT/WHERE query?如何使用 INSERT、SELECT/WHERE 查询执行此操作?

There are the tables.有桌子。

post邮政

+--------+--------+---------+-------------+  
| postid | userid | title   | description |  
+--------+--------+---------+-------------+  
| 1      | 1      | example | example     |  
+--------+--------+---------+-------------+  
| 2      | 2      | example | example     |  
+--------+--------+---------+-------------+  
| 3      | 3      | example | example     |  
+--------+--------+---------+-------------+  

user用户

+--------+----------+--------+  
| userid | username | roleid |  
+--------+----------+--------+  
| 1      | admin    | 1      |  
+--------+----------+--------+  
| 2      | helper   | 1      |  
+--------+----------+--------+  
| 3      | test     | 2      |  
+--------+----------+--------+  

Here is what it should look like after the insertion:这是插入后的样子:

post邮政

+--------+--------+---------+-------------+  
| postid | userid | title   | description |  
+--------+--------+---------+-------------+  
| 1      | 1      | example | example     |  
+--------+--------+---------+-------------+  
| 2      | 2      | example | example     |  
+--------+--------+---------+-------------+  
| 3      | 3      | example | example     |  
+--------+--------+---------+-------------+
| 4      | 2      | Title   | Description |  
+--------+--------+---------+-------------+    

Assuming postid auto-increments:假设 postid 自动递增:

insert into post (userid, title, description)
select userid, "Title", "Description"
from user
where username="helper"

Note that this will not insert anything if the given username is not found.请注意,如果未找到给定的用户名,这将不会插入任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据插入一个表并获取主键作为外键插入另一个表中? - How do I insert data into one table & get the primary key to insert in another table as a foreign key? 如何将多个不同值从一个表中的一列插入到另一个表中并引用相应的外键? - How do i insert multiple varying values from a column in one table to another table with the corresponding foreign key referenced? 如何将一个表的主键值插入另一个表的外键列? - How do I insert primary key value from one table to foreign key column in another? 如何将主键作为外键插入另一个表? - How do I insert the primary key to another table as foreign key? 如何将数据从一个表动态插入到另一个表? - How should I insert data from one table to another dynamically? 当我需要从另一个表中了解外键的值时,如何将数据插入表中? - How can I insert data into a table when I need to know the value of the foreign key from another table? 如何从一个表中选择与表中的列值相匹配的数据? - How do I select data from one table where column values from that table match the conatenated column values of another table? MYSQL仅在该表的列值与另一表的列值匹配的情况下,才如何从该表中选择数据? - MYSQL How do I select data from one table only where column values from that table match the column values of another table? 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM