computermax2328 Posted January 27, 2013 Share Posted January 27, 2013 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?? Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/ Share on other sites More sharing options...
Jessica Posted January 27, 2013 Share Posted January 27, 2013 . $insert = "INSERT INTO archive (firstname, lastname, rank, datefield) VALUES (SELECT firstname, lastname, era, CURDATE() FROM old)"; Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/#findComment-1408586 Share on other sites More sharing options...
Jessica Posted January 27, 2013 Share Posted January 27, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/#findComment-1408588 Share on other sites More sharing options...
computermax2328 Posted January 28, 2013 Author Share Posted January 28, 2013 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, Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/#findComment-1408687 Share on other sites More sharing options...
Barand Posted January 28, 2013 Share Posted January 28, 2013 (edited) It doesn't matter if names differ, just the field types have to be compatible. Note that there is no matching datefield name in the query you are already using Edited January 28, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/#findComment-1408749 Share on other sites More sharing options...
Jessica Posted January 28, 2013 Share Posted January 28, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/273704-insert-select-plus-curdate/#findComment-1408751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.