Coruba67;10879871 wrote:
Based on the quantity I need to put X amount of records into the database. What kind of query can I use to repeat itself X amount of times in MySQL or is there a way to loop it considering its only 1 record on the CSV, or is there a way I can just get the query to loop itself X amount of times?
No there is not.
So you have to get a bit creative.
Upload the csv into a work table.
Now, you need a number sequence. You can do this by creating a table with 0 to the max integer you need. Personally I like to just create a digits table with just 0-9 and then join it to itself to generate the numbers.
SELECT CONCAT(digits.digit, digits1.digit, digits2.digit) FROM digits, digits AS digits1, digits AS digits2;
That exploits the Cartesian join and silent type conversion to return a series of string in the sequence 000 to 999.
Now you can use CAST to then convert the sequence back into INTEGERS
SELECT cast(concat(digits.digit,digits1.digit,digits2.digit) as unsigned) as seq
FROM digits, digits AS digits1, digits AS digits2 order by seq
Now, you can use this as a table-type subquery in an insert query to add the number of rows you want. Assuming you have loaded you csv into a table called work with 2 columns partno, quantity
INSERT INTO stock(partno) SELECT partno FROM work, (SELECT cast(concat(digits.digit,digits1.digit,digits2.digit) as unsigned) as seq
FROM digits, digits AS digits1, digits AS digits2 order by seq) AS counter
WHERE counter.seq < work.quantity
And there it is all in 1 query. What it does is join each part no to the 0-999 sequence, so you would get each part no repeated 1000 times. The where clause then limits the repetition of each part to its quantity.
Seems a bit complex. Well so does a php code loop that is going to run umpteen queries in a loop to insert 1 row at a time: and the php code would take longer to type as well. Imagine how long it would take to process if you had 100 parts with 50 items each in stock, a modest inventory to my mind, but that would be 5000 queries.
Any time someone suggests you run a query in a loop ignore them - they do not know anything about SQL. In 25 years of using it I have come across maybe 5 occasions where running a query in a loop was the right answer - because it was the only answer.
SQL is a very elegant and powerful programming language designed to work on large numbers of records. Explore it, it can do a great many things very well and deserves as much effort to master as any other programming language such as C or PHP or VB.
Having said all this, I now ask the question - Why do you need to do it? Normal DB design would say that you just need 1 row per partno, with the quantity field for stock on hand. Please explain your rational for this design.