如何获取在mysql INSERTed行的ID? [英] How to get the ID of INSERTed row in mysql?

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

问题描述

我使用此命令将一些单词插入到两列表中...

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




  1. 如何获取每个字被插入的行的ID(主键)。我的意思是在INSERT后返回一个像55,56,57的值。 MySQL有这样的响应吗?


  2. 术语列是UNIQUE。如果某个术语已存在,则不会插入它。可以返回此重复的引用(即,术语存在的行的ID)。



解决方案 div>


  1. 您通过 SELECT LAST_INSERT_ID(); 或通过框架/ MySQL库(无论什么语言)调用 mysql_insert_id() 。 p>


  2. 这不会工作。您必须在插入后查询ID。



I am INSERTing some words into a two-column table with this command...

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

  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 INSERT. Does MySQL have such a response?

  2. The term column is UNIQUE. If a term already exists, it 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. You get it via SELECT LAST_INSERT_ID(); or via having your framework/MySQL library (in whatever language) call mysql_insert_id().

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

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

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