Click to See Complete Forum and Search --> : Insert Same Data Twice in TABLE


bullsb
02-01-2009, 05:35 AM
Hi,

I need to INSERT same data (Having the uniq ID and change in FName) twice in the same TABLE.

I am getting ID, FName from a Table Called Emp_Details (Pulling using SELECT) and ID, FName from the previous Page.

I want to INSERT this in to the same TABLE at a time. E.G.

ID - 428
FName - Badri
The above getting from DATABASE and below from previous page

ID - 428
FName - Shiva

Note - ID is same for both the FName. The point here is how to INSERT. Plz help

chazzy
02-01-2009, 08:53 AM
huh? why not just run the same insert?

BigDoggy59
02-01-2009, 09:30 AM
What you need to do is change your table to use a multi-column key...


ALTER TABLE fred DROP PRIMARY KEY, ADD PRIMARY KEY(`ID`, `FName`);


Depending on setup, you may want to split the above into two queries, but the end result is the same. Just looking at `ID` disallows the second input.

bullsb
02-01-2009, 09:55 AM
Friends,

Can I insert more than 2 records @ a time. Again, the ID is going to be uniq and the Name Changes.

Actually, Its for a billing software to generate the final bill based on the supply made different times. It goes like this...the user will supply goods to his clients/vendors and once in 15 days or so he will generate bill...sometimes the user would have supplied only one goods or more than one goods. However, the final bill will have all the goods supplied...Hope I m clear in what I conveyed?

BigDoggy59
02-01-2009, 11:46 AM
So what you want is to INSERT multiple records with one statement, correct?

The trick is to list each group of values in comma-separated groups of parentheses.

INSERT INTO `myTable` (`ID`, `FName`) VALUES
('ID1', 'file1.txt'),
('ID2', 'file2.txt'),
('ID3', 'file3.txt')

bullsb
02-02-2009, 02:16 AM
Thanks, It worked out well as i wanted.