destined Posted June 9, 2006 Share Posted June 9, 2006 Thanks for lookingIam useing phpMyAdmin and tryin to run a querytables: newsletter, emailsI am getteing this#1064 - 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 ' firstname)' at line 2 I have tryed a few variations of this I know this is a simple thing to most but not to me Please helpINSERT INTO emails (email, name)SELECT * FROM newsletter WHERE (emailaddress, firstname)i tryed closing it with "; Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/ Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 You need:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]`emails`[/color] (email, name)[span style=\'color:blue;font-weight:bold\']SELECT[/span] `emailaddress`, `firstname` [color=green]FROM[/color] [color=orange]`newsletter`;[/color] [!--sql2--][/div][!--sql3--]That will insert the firstname and emailaddress from each row in the newsletter table into a new row in the emails table. Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43674 Share on other sites More sharing options...
destined Posted June 9, 2006 Author Share Posted June 9, 2006 [!--quoteo(post=381888:date=Jun 9 2006, 11:23 AM:name=Fyorl)--][div class=\'quotetop\']QUOTE(Fyorl @ Jun 9 2006, 11:23 AM) [snapback]381888[/snapback][/div][div class=\'quotemain\'][!--quotec--]You need:[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']INSERT[/span] [color=green]INTO[/color] [color=orange]`emails`[/color] (email, name)[span style=\'color:blue;font-weight:bold\']SELECT[/span] `emailaddress`, `firstname` [color=green]FROM[/color] [color=orange]`newsletter`;[/color] [!--sql2--][/div][!--sql3--]That will insert the firstname and emailaddress from each row in the newsletter table into a new row in the emails table.[/quote]Thank you for the quick replyGod bless Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43677 Share on other sites More sharing options...
joquius Posted June 9, 2006 Share Posted June 9, 2006 you have to do it this way however:[code]INSERT INTO `emails` (email, name) VALUES ((SELECT `emailaddress`, `firstname` FROM `newsletter` WHERE `id` = '$id'), (SELECT `emailaddress`, `firstname` FROM `newsletter` WHERE `id` = '$id')) WHERE `id` = '$id'[/code]assuming you want to copy accross the values. the selected rows will have to be single rows and single results within VALUES () Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43681 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 [!--quoteo(post=381895:date=Jun 9 2006, 10:35 AM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 9 2006, 10:35 AM) [snapback]381895[/snapback][/div][div class=\'quotemain\'][!--quotec--]you have to do it this way however:[code]INSERT INTO `emails` (email, name) VALUES ((SELECT `emailaddress`, `firstname` FROM `newsletter` WHERE `id` = '$id'), (SELECT `emailaddress`, `firstname` FROM `newsletter` WHERE `id` = '$id')) WHERE `id` = '$id'[/code]assuming you want to copy accross the values. the selected rows will have to be single rows and single results within VALUES ()[/quote]Actually, that's not necessary. My method will work perfectly. SQL will insert each row returned by the SELECT statement (as long as the column names match up, which they do in my example), so it doesn't need to return single rows within VALUES(). In fact, VALUES is not needed at all. Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43685 Share on other sites More sharing options...
joquius Posted June 9, 2006 Share Posted June 9, 2006 ok ok ok it's not a one line query Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43690 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]ok ok ok it's not a one line query[/quote]Even if it was, my method would still work. Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43696 Share on other sites More sharing options...
destined Posted June 9, 2006 Author Share Posted June 9, 2006 ok ok ok ...loli used the first method before comming back here and it work for what i needed to doBut now i need to get the same emailaddress/firstname from an other database (users)table: tbl_usersThank You both for your help Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43704 Share on other sites More sharing options...
Fyorl Posted June 9, 2006 Share Posted June 9, 2006 I'm not so sure about this one. You'd probably have to use PHP, can't think of any way of doing it from PHPMyAdmin Quote Link to comment https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43745 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.