I want to be able to take a record from student A and modify it slightly and replace a few things in the record of a student B with the modified info from student A. Does that make sense? Essentially I am taking everything from student A except for pntr and studentPntr, changing the percentComplete, courseStatus and percentComplete fields, and replacing student B's record with that information.
Here is the code of what I am trying to do now with a REPLACE...SELECT:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''38', coursePntr, percentComplete, startDateTime, endDateTime,' at line 1
From the look of the error I am assuming I cannot $newPntr in the REPLACE part of the query but it was the only thing I could think of to get this to work how I want it to.
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Using REPLACE is really not necessary here, since you aren't replacing the original row; you're just inserting a row based on another row's data. You can use the same INSERT INTO table SELECT syntax (assuming your version of MySQL is > 4.0.14).
I don't want to insert a new row, though, as this query is only going to be executed if the student already has a record in the table for that particular class. I want to replace that data with the new data I got from messing around with the other students data.
The percentComplete part of the select should only be 83.333 and the timeSpent should only be 2398 but that really isn't that important. For whatever reason though it is giving me outrageous values for those two fields. That is an entirely different issue though.
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
OK I believe the problem (which I should have caught before) is you're listing one of the column names to insert into as '38', where it should be the name of the column you're wanting to insert '38' into. It's what you mentioned in the first post, that $newpntr deal.
If I understand you right, the column 'pntr' is your primary key, and you want to replace the existing row where pntr = '38' with this other data, so I think you can just remove the $newpntr from the into() area, and in the select area, use the literal '38' instead of column name pntr. That should load up '38' into column pntr as the row you are replacing.
If I understand you right, the column 'pntr' is your primary key, and you want to replace the existing row where pntr = '38' with this other data, so I think you can just remove the $newpntr from the into() area, and in the select area, use the literal '38' instead of column name pntr. That should load up '38' into column pntr as the row you are replacing.
You are right, the column 'pntr' is my primary key, but it is just an auto-increment value so it's value is not important to the actually record really.
I guess to make it more clear as to what I need it to do I will just say I want to take the info from a course that student A has/is taking and copy it to student B. In order to do this, student B must also have a record for this course i.e. they must already be enrolled in order for us to get the payment. I want to replace all the values in student Bs record except for pntr, studentPntr. Those two will need to stay the same.
These are the two rows in the table I am working with in my example right now:
These are what the two should look like after I am done (the only things that will change are percentComplete and timeSpent but for this example they aren't important so I just copied them over without modification):
That is what I was originally going to go with but my boss told me to check out REPLACE so thats why I have been trying it out. It looks like I was correct in my thinking before hand.
Thanks a lot for the help! Hopefully this works and I can leave you alone.
As for REPLACE INTO, its fine to use basically on anything that is 'user updated' ie anything that is taking place at the speed of a human being's update capability - if you're updating thousands of records with it in an automated process or have hundreds of users updating records, then the performance hit might be noticeable.
That is ofcourse assuming your tables are indexed correctly