Skip to main content
John_Allred
Inspiring
July 17, 2021
Answered

Could I get some quick guidance on inserting multiple rows - CF to (MySQL)?

  • July 17, 2021
  • 1 reply
  • 307 views

My question is whether there's a simpler way to do the following than what I've discovered on my own.

 

My user will run a query and make selections of items (MID) from a table. These MIDs will be stored in another table along with the user's UserID. This will allow the user to tailor another query to be populated only with these MIDs. The user might make one set of selections and then replace it later with a different set of selections. 

 

Right now, I'm not worrying about how to code the CFML to produce the query I send to MySQL. I'm just wanting to know if this is a suitable/optimal method of doing it.

 

tbl_user_units has three columns, UUID (autoincrement), UserID, MID.

I'm using 202 for the UserID in this example

The user is likely to select between 20 and 40 MIDs

I would use a form with checkboxes to select the MIDs

 

Not knowing how to delete old selections and insert new ones in a single operation, I propose to first  delete all the old values using this:

 

 

DELETE from tbl_user_units
WHERE userid = 202;

 

 

 

Then, insert the newly selected MIDs (for example, 224, 225, 226):

 

 

INSERT INTO tbl_user_units (userid,mid)
VALUES
(202,224),
(202,225),
(202,226),
;

 

 

 

I could store the selections in a single field in the tbl_user table as a list, e.g., Units = [224,225,226]. I'm inexperienced with lists and arrays. Would it be simpler to read the list from this field and use it to create my query? This would obviate the need for a separate table.

 

Thanks for your feedback!

John

This topic has been closed for replies.
Correct answer John123

Your proposal to use the delete then insert is a good solution as it avoids having to determine which records should be deleted & inserted, much simpler code. I would however wrap the delete and insert in a transaction to keep the maintenance as one unit.

 

As for storing the list of MID's on the user record, it really depends on how you are going to use the data. If you ever want to join to some other table from the MID's then you can only do that if they are in a separate table. If you never want to join then either way will work as long as you have a field large enough to store the longest list of MID's. You could store the list on the user table then use the IN SQL operation to select MID records from your list.

 

 

1 reply

John123Correct answer
Participating Frequently
July 19, 2021

Your proposal to use the delete then insert is a good solution as it avoids having to determine which records should be deleted & inserted, much simpler code. I would however wrap the delete and insert in a transaction to keep the maintenance as one unit.

 

As for storing the list of MID's on the user record, it really depends on how you are going to use the data. If you ever want to join to some other table from the MID's then you can only do that if they are in a separate table. If you never want to join then either way will work as long as you have a field large enough to store the longest list of MID's. You could store the list on the user table then use the IN SQL operation to select MID records from your list.

 

 

John_Allred
Inspiring
July 20, 2021

Thanks, John. That was exactly the sort of feedback I was looking for.

8-)