Jump to content

Insert and update between two tables help


bnovak

Recommended Posts

I think this should be an easy one for someone smarter than me to figure out. I'm trying to do this in phpmyadmin for now, but will convert it to a php function once I get it testing and working.

 

can't do show create table as it's being auto-generated from a user system.

 

basically two tables:

 

profile

id(PK), stateLicenses,additionalLicenses (there's more but for this instance I'm not worried about the other fields)

 

and

state_licenses

id(pk), selectStates,selectSecondary

 

basically want to make a backup of the id,stateLicenses and additionalLicense fields from the profile table to the state_licenses table.

 

If I start with an EMPTY state_licenses table, I can insert all of the details I need into this table with the

 

 INSERT INTO state_licenses (id,selectStates,selectSecondary) SELECT id,stateLicenses,additionalLicenses FROM profile 

 

I know there needs to be an

 

UPDATE state_licenses SET id,selectStates,selectSecondary='profile.id,profile.stateLicenses,profile.additionalLicenses' where ??? 

 

It's that update line that I can't figure out and the tutorials I've found are all for updating inside a table, not updating from another table.

 

what I would like to do is update selectState and selectSecondary in the state_licenses table if there is already an id in the table, and if not, insert a new record with the id, stateLicenses, additionalLicenses from the profile field (to cover increasing profile id's)

 

basically - if state_license(id) is not null update record, else insert the new record.

 

for the life of me I can't figure out how to update the pre-existing records based on the info from the profile table and I'm (obviously) green with coding this stuff.

 

does this make any sense?

 

Unfortunately, I can't come up with any good work around. Basically the member system I'm using is resetting my column properties to VARCHAR(150) when I need TEXT so I need a way to make a backup of user generated info.

 

Any help is greatly appreciated.

 

Thank

-BN

 

Link to comment
Share on other sites

Hi there,

 

I would probably do

$row_exists = db_query("SELECT CASE WHEN EXISTS(SELECT * FROM profile WHERE id = " . db_quote($id) . ") THEN 1 ELSE 0 END");
if ($row_exists) {
  db_query("UPDATE state_licenses SET selectStates = profile.stateLicenses, selectSecondary = profile.additionalLicenses FROM profile WHERE state_licenses.id = profile.id AND profile.id = " . db_quote($id));
} else {
  //insert, you know that one.
}

 

It's called an UPDATE...FROM

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.