Jump to content

Insert Select plus curdate()


computermax2328

Recommended Posts

Hello All,

 

I have a data table with the columns firstname, lastname and rank. I am moving those values from that table into an archive table and the archive table has the same structure, but an additional date on the end. Obviously to tell the different between the repeating values.

 

I have a query like this......

$insert = "INSERT INTO archive (firstname, lastname, rank) VALUES (SELECT firstname, lastname, era FROM old)";

 

How can I add the mysql command CURDATE() into that insert command? Can I add parentheses inside of the parentheses around the SELECT command??

 

Thanks in advance??

Link to comment
Share on other sites

Actually this is better sorry

 

.

$insert = "INSERT INTO archive (firstname, lastname, rank, datefield)
SELECT firstname, lastname, era, CURDATE() FROM old";

 

 

I don't think you can use values the way you were, it'll just do one row.

Link to comment
Share on other sites

Thanks for getting back to me. One more quick question. I used the technique you showed me above and it worked well, but I was wondering, does it matter if the INSERT columns are not the same as the SELECT columns? The only difference is they have different names. Everything else is the same, storage type and all.

 

Thanks,

Link to comment
Share on other sites

As Barand said it doesn't matter. Think about this - you can do a SELECT of something that isn't a column name. Try SELECT 'foo' FROM table. You can select a string you type, you can select CURDATE() or NOW(), you can SELECT 5-2. From a table or not sometimes. You can select them with aliases. It doesn't matter for an insert. 

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.