How do I get the ID of multiple inserted rows in MySQL?(如何在 MySQL 中获取多个插入行的 ID?)
问题描述
我正在使用以下命令将一些单词插入到一个两列的表格中:
I am inserting some words into a two-column table with this command:
INSERT IGNORE INTO terms (term) VALUES ('word1'), ('word2'), ('word3');
如何获取插入每个单词的行的 ID(主键).我的意思是在执行
INSERT后返回一个像55,56,57"这样的值.MySQL有这样的反应吗?
How can I get the ID (Primary Key) of the row in which each word is inserted. I mean returning a value like "55,56,57" after executing
INSERT. Does MySQL have such a response?
术语列是UNIQUE.如果一个术语已经存在,MySQL 不会插入它.是否可以返回此重复项的引用(即该术语所在行的 ID)?像55,12,56"这样的回复.
The term column is UNIQUE. If a term already exists, MySQL will not insert it. Is it possible to return the reference for this duplication (i.e. the ID of the row in which the term exists)? A response like "55,12,56".
推荐答案
您可以通过
SELECT LAST_INSERT_ID 获得它();或通过让您的框架/MySQL 库(任何语言)调用mysql_insert_id().
那行不通.在那里你必须在插入后查询 ID.
That won't work. There you have to query the IDs after inserting.
这篇关于如何在 MySQL 中获取多个插入行的 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 MySQL 中获取多个插入行的 ID?
基础教程推荐
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
