使用 LAST_INSERT_ID 插入 n 行
我有 3 个表,分别名为 POSTS、HASHTAGS 和 POSTS_HASHTAGS_RELATION,如下所示。
CREATE TABLE POSTS(
post_id int unsigned NOT NULL AUTO_INCREMENT,
content varchar(200) NOT NULL,
PRIMARY KEY (post_id)
);
CREATE TABLE HASHTAGS(
hashtag_id int unsigned NOT NULL AUTO_INCREMENT,
hashtag varchar(40) NOT NULL,
PRIMARY KEY (hashtag_id)
);
CREATE TABLE POSTS_HASHTAGS_RELATION(
post_id int unsigned NOT NULL,
hashtag_id int unsigned NOT NULL,
PRIMARY KEY (post_id, hashtag_id)
);
用户发帖时,会从 HASHTAGS 中保存的主题标签中选择最多 20 个主题标签。我将这些主题标签的 hashtag_id 作为逗号分隔字符串从前端发送到后端,在后端将其转换为 Nodejs 中的列表。
首先,是否有更好的方法来构建这个?
其次,如何在单个查询中向 POSTS_HASHTAGS_RELATION 插入可变数量的行?
INSERT INTO POSTS (content) VALUES ('bla bla bla bla');
SET @post_id = LAST_INSERT_ID();
INSERT INTO POSTS_HASHTAGS_RELATION (post_id, hashtag_id) VALUES (@post_id, 19), (@post_id, 41) ...;
// Something like this but the number of values can be between 1 and 20
如果之前已回答过这个问题,只需引导我找到这个答案。我找不到任何相关的东西。我认为这不是一个非常独特的问题。
I have 3 tables called POSTS, HASHTAGS and POSTS_HASHTAGS_RELATION, as below.
CREATE TABLE POSTS(
post_id int unsigned NOT NULL AUTO_INCREMENT,
content varchar(200) NOT NULL,
PRIMARY KEY (post_id)
);
CREATE TABLE HASHTAGS(
hashtag_id int unsigned NOT NULL AUTO_INCREMENT,
hashtag varchar(40) NOT NULL,
PRIMARY KEY (hashtag_id)
);
CREATE TABLE POSTS_HASHTAGS_RELATION(
post_id int unsigned NOT NULL,
hashtag_id int unsigned NOT NULL,
PRIMARY KEY (post_id, hashtag_id)
);
When user posts, they select upto 20 hashtags from those saved in HASHTAGS. I send the hashtag_id(s) of these hashtags from front end as comma separated string to backend where it is converted to list in nodejs.
Firstly, is there a better approach to struture this?
Secondly, how do I insert variable number of rows to POSTS_HASHTAGS_RELATION in a single query?
INSERT INTO POSTS (content) VALUES ('bla bla bla bla');
SET @post_id = LAST_INSERT_ID();
INSERT INTO POSTS_HASHTAGS_RELATION (post_id, hashtag_id) VALUES (@post_id, 19), (@post_id, 41) ...;
// Something like this but the number of values can be between 1 and 20
If this has been answered befored, just guide me to that answer. I am unable to find anything relevant. I assume this is not a very unique problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您显示的是我这样做的方式。您可以使用显示的方式插入多行,通过在值关键字之后编写多个行构造函数。如果这样做,则必须在每个行构造器中插入语句中指定的所有列中包含一个值。因此,您必须在每个行构造函数中引用
@post_id
变量。如果您真的不喜欢多次编写
@post_id
,那么您可以做这样的事情:但是,看起来 都比您这样做的方式更清晰且可读。
您的评论:
我不是Node.js程序员,但是我使用其他语言的技术来构建基于输入列表的许多行构造函数的SQL语句。适当的查询参数只能代替标量值,而不是列表,表达式或标识符或SQL关键字等。但是我理解Node.js可以做一些额外的字符串订阅魔法,因此他们并没有真正执行查询参数。
假设您刚刚将插入插入到帖子中,您可以捕获最后的插入ID:
然后为下一个插入创建部分插入语句:
您将需要一个行构造函数的数组,而对于参数,则需要一个数组:
现在您有一个数组行构造函数,
toString()
将变成逗号分隔的字符串。而且您有一系列值以作为参数传递。我猜
.toString()
是可选的,因为该数组应通过将其连接到insert
字符串来自动胁迫到字符串。同样,我不是一个节点。但这应该给您这个技术的想法。
What you show is the way I would do it. You can insert multiple rows using INSERT in the manner you show, by writing multiple row constructors after the VALUES keyword. If you do, you must include a value for all the columns named in your INSERT statement in every row constructor. Therefore you must reference the
@post_id
variable in each row constructor.If you really don't like to write
@post_id
more than once, you could do something like this:But that seems less clear and readable than the way you were doing it.
Re your comment:
I'm not a node.js programmer, but I've used the technique in other languages to build an SQL statement with a number of row constructors based on the input list. Proper query parameters can only be used in place of scalar values, not lists or expressions or identifiers or SQL keywords, etc. But I understand node.js does some extra string-substitution magic, so they're not really doing query parameters.
Suppose you had just done your INSERT into POSTS, you could capture the last insert id:
Then create a partial INSERT statement for your next insert:
You will need an array for the row constructors and an array for the parameters:
Now you have an array of row constructors, which
toString()
will turn into a comma-separated string. And you have an array of values to pass as parameters.I guess the
.toString()
is optional, because the array should be coerced to a string automatically by concatenating it to theinsert
string.Again, I'm not a node.js programmer, so forgive me if there are errors or style problems. But that should give you the idea of this technique.
将数据以 SP 格式保存到表中:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a9c622ea7ad2dd48dba18312c7a33487(故意使用草率的 CSV)。
Saving the data into the tables in SP format:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a9c622ea7ad2dd48dba18312c7a33487 (sloppy CSV is used on purpose).