问题
I have one table named "questions". This table may have multiple questions. Here I am looping over the insert query to insert all question values for having duplicate questions. It is successfully adding questions, but the issue is I can't get the list or array of recently inserted question records ids.
Here is the code.
<cfloop query="questions">
<cfquery name="insertDuplicatequestions">
INSERT INTO questions (
survey_Id, questiontype_Id, question, rank, isrequired
)
VALUES
(
<cfqueryparam cfsqltype="cf_sql_bigint" value="#getSurveyid.id#">
, <cfqueryparam cfsqltype="cf_sql_bigint" value="#questions.questiontype_Id#">
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#questions.question#">
, <cfqueryparam cfsqltype="cf_sql_numeric" value="#questions.rank#">
, <cfqueryparam cfsqltype="cf_sql_integer" value="#questions.isrequired#">
)
<cfif questions.CurrentRow LT questions.RecordCount>,</cfif>
</cfquery>
<cfquery name="getQuestionid">
SELECT LAST_INSERT_ID() AS id
</cfquery>
<cfset response = getQuestionid.id>
</cfloop>
I have tried with LAST_INSERT_ID(), but it retrieves last and single record id. Could anyone please let me know if there is anyway to figure out this. Thanks.
回答1:
(More of an addendum, but it is too long for comments ... )
Like RandomSeed said, capturing multiple record id's generated by an insert is not supported in MySQL. One of the alternatives he described is inserting the records individually, and capturing the new ID on each iteration. Here is a rough outline of how you might do that using cfquery's result attribute.
<!--- initialize array that will store the new ids -->
<cfset idArray = []>
<!--- use a transaction to ensure ALL of the inserts succeed or fail together -->
<cftransaction>
<cfloop ...>
<!--- use the "result" attribute to capture the new id --->
<cfquery result="addRecord" ....>
INSERT INTO YourTable (...) VALUES (...);
</cfquery>
<!--- save the id in the array -->
<cfset arrayAppend(idArray, addRecord.GENERATED_KEY)>
</cfloop>
</cftransaction>
<!--- display new id's --->
<cfdump var="#idArray#">
As an aside, while you can use LAST_INSERT_ID(), it should normally be placed inside the same cfquery tags as the INSERT to guaranteed you get the last id for the current connection. Note: For it to work, the datasource settings must allow multiple statements (not enabled by default).
回答2:
There is no native way to do it. Your best call would be adding a timestamp column to your table, representing the insertion date (by default, a TIMESTAMP field takes NOW() as a default value).
Alternatively, you could just check the result of each insertion from within your application code (eg. with LAST_INSERT_ID()). Then build the array in your application code.
回答3:
last_insert_id() works as it's name: you get the ID of the LAST record you inserted. If you're inserting multiple records, then you'll have to do a last_insert_id() for EVERY record. There is no way to retrieve the IDs of the previous inserts - those are lost.
来源:https://stackoverflow.com/questions/18514689/how-can-i-get-an-array-of-recently-inserted-records-ids