lastinsertid

can SQL insert using select return multiple identities? [duplicate]

别来无恙 提交于 2019-11-30 18:01:48
This question already has an answer here: How to insert multiple records and get the identity value? 7 answers I am insertnig into a table using a selection INSERT california_authors (au_id, au_lname, au_fname) SELECT au_id, au_lname, au_fname FROM authors WHERE State = 'CA' say i have an identity column in california_authors . Can i get all the ids inserted by the above particular insertion just like i can get @@IDENTITY for last single insertion ? I can't use select command for california_authors as there may exists previously inserted records with filter State = 'CA' You can use the output

MySQL and the chance of the wrong id being returned by LAST_INSERT_ID()

♀尐吖头ヾ 提交于 2019-11-30 17:37:54
From what I have read LAST_INSERT_ID() retrieves the id of the last insert statement to run. If that the last insert statement run on your connection or the last insert run on the database for all connections? I guess what I am trying to ask is: on a busy database driven website what are the chances the following code would return the wrong id, and is there a method to prevent that other then locking the database? INSERT INTO example (string) VALUE ('something'); SELECT LAST_INSERT_ID(); Would PHP's mysql_insert_id() be a better solution, or should I just query for data that was just inserted

can SQL insert using select return multiple identities? [duplicate]

点点圈 提交于 2019-11-30 01:41:10
问题 This question already has answers here : How to insert multiple records and get the identity value? (7 answers) Closed 3 years ago . I am insertnig into a table using a selection INSERT california_authors (au_id, au_lname, au_fname) SELECT au_id, au_lname, au_fname FROM authors WHERE State = 'CA' say i have an identity column in california_authors . Can i get all the ids inserted by the above particular insertion just like i can get @@IDENTITY for last single insertion ? I can't use select

PDO lastInsertId() always return 0

我的未来我决定 提交于 2019-11-29 09:17:52
I've come across with a problem. My framework was working just fine with PHP 5.3.0. I upgraded my PHP version to PHP 5.4.x and I started to have few issues with some parts of my framework. After PHP version upgrade, PDO lastInsterId() always returns 0 . I have auto-increment field called id . It is adding the data to database without any problems. For some reason I keep getting 0 as last insert id. Here is my code; databaseobjects.php public static function create () { global $db; $attributes = self::sanitize(static::$fields); $sql = "INSERT INTO ".PREFIX.static::$table_name." ("; $sql .= join

Mysql mulitple row insert-select statement with last_insert_id()

拟墨画扇 提交于 2019-11-29 00:13:50
Ok. So the short of it is, I was trying to do an INSERT SELECT such as: START TRANSACTION; INSERT INTO dbNEW.entity (commonName, surname) SELECT namefirst, namelast FROM dbOLD.user; SET @key = LAST_INSERT_ID(); INSERT INTO dbNEW.user (userID, entityID, other) SELECT user_id, @key, other FROM dbOLD.user; COMMIT; Of course @key does not return each subsequent LAST_INSERT_ID() from each insert but the ID from only the last insert. Basically, I'm splitting an old USER Table into an ENTITY and USER like: dbOLD.user +-------------+---------------------+------+-----+------------+----------------+ |

Get the last insert id with doctrine 2?

假如想象 提交于 2019-11-28 17:42:26
How can I get the last insert id with doctrine 2 ORM? I didn't find this in the documentation of doctrine, is this even possible? I had to use this after the flush to get the last insert id: $em->persist($user); $em->flush(); $user->getId(); clarkstachio You can access the id after calling the persist method of the entity manager. $widgetEntity = new WidgetEntity(); $entityManager->persist($widgetEntity); $entityManager->flush(); $widgetEntity->getId(); You do need to flush in order to get this id. Syntax Error Fix: Added semi-colon after $entityManager->flush() is called. Francesco Casula If

pdo lastInsertId returns zero(0)

一个人想着一个人 提交于 2019-11-28 14:00:54
All queries execute successfully, when I check table in MySQL row inserted successfully without any error, but lastInsertId() returns 0 . why? My code: // queries executes successfully, but lastInsetId() returns 0 // the menus table has `id` column with primary auto_increment index // why lastInsertId return 0 and doesn't return actual id? $insertMenuQuery = " SELECT @rght:=`rght`+2,@lft:=`rght`+1 FROM `menus` ORDER BY `rght` DESC limit 1; INSERT INTO `menus`(`parent_id`, `title`, `options`, `lang`, `lft`, `rght`) values (:parent_id, :title, :options, :lang, @lft, @rght);"; try { // menu sql

PDO lastInsertId issues, php

家住魔仙堡 提交于 2019-11-28 13:34:06
I have tried lots of ways to get the last inserted ID with the code below (snipplet from larger class) and now I have given up. Does anyone know howto get PDO lastInsertId to work? Thanks in advance. $sql = "INSERT INTO auth (surname, forename, email, mobile, mobilepin, actlink, regdate) VALUES (:surname, :forename, :email, :mobile, :mobpin, :actlink, NOW())"; $stmt = $this->dbh->prepare($sql); if(!$stmt) { return "st"; } $stmt->bindParam(':surname', $this->surname); $stmt->bindParam(':forename', $this->forename); $stmt->bindParam(':email', $this->email); $stmt->bindParam(':mobile', $this-

lastInsertId does not work in Postgresql

北城以北 提交于 2019-11-28 12:11:40
I am using Postgresql, when I want to use PDO to retrieve the latest insertion ID, I got a problem. Here is my code: $db->lastInsertId('columnName'); The error message says SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "columnName" does not exist I guess I have some misunderstanding about "sequence object" stated in the PHP Manual. Note: Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver. For example, PDO_PGSQL() requires you to specify the name of a sequence object for the name parameter. Currently, the "columnName" is

What happens when auto_increment on integer column reaches the max_value in databases?

风格不统一 提交于 2019-11-28 04:27:30
I am implementing a database application and I will use both JavaDB and MySQL as database. I have an ID column in my tables that has integer as type and I use the databases auto_increment-function for the value. But what happens when I get more than 2 (or 4) billion posts and integer is not enough? Is the integer overflowed and continues or is an exception thrown that I can handle? Yes, I could change to long as datatype, but how do I check when that is needed? And I think there is problem with getting the last_inserted_id()-functions if I use long as datatype for the ID-column. Jim Martin's