如何获取MySQL中多个插入行的ID? [英] How do I get the ID of multiple inserted rows in MySQL?

查看:210
本文介绍了如何获取MySQL中多个插入行的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下命令将单词插入到两列表中:

I am inserting some words into a two-column table with this command:

INSERT IGNORE INTO terms (term) VALUES ('word1'), ('word2'), ('word3');

  1. 如何获取插入每个单词的行的ID(主键).我的意思是执行INSERT后返回"55,56,57"之类的值. MySQL有这样的响应吗?

  1. 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".

推荐答案

  1. 您可以通过 SELECT LAST_INSERT_ID(); 或通过使用框架/MySQL库(使用任何语言)调用那是行不通的.插入后,您必须在那里查询ID.

    That won't work. There you have to query the IDs after inserting.

    这篇关于如何获取MySQL中多个插入行的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆