Re: [PHP-DB] Combining INSERT...SELECT and INSERT INTO
On Tuesday 15 January 2002 16:04, you wrote: On 1/14/2002 12:54 PM +0200 Markus Lervik wrote: Is there any way to combine INSERT...SELECT and INSERT INTO so one could insert a new row with a few values inserted by hand and others from another table? something like INSERT INTO mag_table (mag_id,issn,year,info,volume,numbers,remarks) VALUES ( (SELECT id FROM mag_names WHERE mag_names.name=Asdf), 1234-5678,2001,foobar,1-4,1-52,foobar); Try doing something like: INSERT INTO mag_table (mag_id,issn,year,info,volume,numbers,remarks) SELECT mag_names.id as mag_id, '1234-5678' as issn, '2001' as year, 'foobar' as info, '1-4' as volume, '1-52' as numbers, 'foobar' as remarks, FROM mag_names WHERE mag_names.name='Asdf'; I don't know if that will work as-is, but something along those lines should give you what you need. Well, here's how I ended up doing it before I red your mail: $concat_string = SELECT CONCAT_WS(\,\,\; if(isset($inputName)) { if(!$result=mysql_query(SELECT id FROM mag_names WHERE mag_names.name=$inputName)) { die(error); } if(!$row=mysql_fetch_assoc($result)) { if(!$result=mysql_query(INSERT INTO mag_nimes (name) VALUES \$inputName\)) { die(error); } elseif(!$result=mysql_query(SELECT id FROM mag_names WHERE mag_names.name=$inputName)) { die(error); } } $row=mysql_fetch_assoc($result); $concat_string .= Name=\\\ . $row[id] . ,\; } Then concatenate Name, ISSN and everything else to construct the end of my nice little UPDATE mag_table SET ... -sentence, pass it through mysql and concatenate the UPDATE sentece and the result from the previous mysql query, stick a little duct tape here, a bit of super glue there and a few five-inch nails and stuff the whole thing down mysql. : ) Well, it works. : ) Cheers, Markus -- Markus Lervik Linux-administrator with a kungfoo grip Vaasa City Library - Regional Library [EMAIL PROTECTED] +358-6-325 3589 / +358-40-832 6709 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] Combining INSERT...SELECT and INSERT INTO
if you are using mysql then no i don't think you can do that, since it doesn't support nested queries - postgres or some other db may well support it tho but like miles said - give it a go :) (maybe on a test database/table just in case) hth -b // -Original Message- // From: Markus Lervik [mailto:[EMAIL PROTECTED]] // Sent: Monday, 14 January 2002 6:54 PM // To: [EMAIL PROTECTED] // Subject: [PHP-DB] Combining INSERT...SELECT and INSERT INTO // // // Hello! // // It's me again, with the magazine database : ) // // Is there any way to combine INSERT...SELECT and INSERT INTO so // one could insert a new row with a few values inserted by // hand and others // from another table? something like // // INSERT INTO mag_table (mag_id,issn,year,info,volume,numbers,remarks) // VALUES ( (SELECT id FROM mag_names WHERE mag_names.name=Asdf), // 1234-5678,2001,foobar,1-4,1-52,foobar); // // I'm trying to construct myself a nice clever INSERT statement to my // web-frontend. Haven't got it to work, though. Is this even possible? // The other option would be to use three(?) mysql_query calls // from my PHP-code, // but I'd like to keep it as simple as possible. // // Cheers, // Markus // // -- // Markus Lervik // Linux-administrator with a kungfoo grip // Vaasa City Library - Regional Library // [EMAIL PROTECTED] // +358-6-325 3589 / +358-40-832 6709 // // -- // PHP Database Mailing List (http://www.php.net/) // To unsubscribe, e-mail: [EMAIL PROTECTED] // For additional commands, e-mail: [EMAIL PROTECTED] // To contact the list administrators, e-mail: // [EMAIL PROTECTED] // -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] Combining INSERT...SELECT and INSERT INTO
On 1/14/2002 12:54 PM +0200 Markus Lervik wrote: Is there any way to combine INSERT...SELECT and INSERT INTO so one could insert a new row with a few values inserted by hand and others from another table? something like INSERT INTO mag_table (mag_id,issn,year,info,volume,numbers,remarks) VALUES ( (SELECT id FROM mag_names WHERE mag_names.name=Asdf), 1234-5678,2001,foobar,1-4,1-52,foobar); Try doing something like: INSERT INTO mag_table (mag_id,issn,year,info,volume,numbers,remarks) SELECT mag_names.id as mag_id, '1234-5678' as issn, '2001' as year, 'foobar' as info, '1-4' as volume, '1-52' as numbers, 'foobar' as remarks, FROM mag_names WHERE mag_names.name='Asdf'; I don't know if that will work as-is, but something along those lines should give you what you need. Cheers, Toby -- Tobyn Baugher [EMAIL PROTECTED] http://www.cartoonviolence.net/ -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]