Jump to content

Recommended Posts

Thanks for looking
Iam useing phpMyAdmin and tryin to run a query
tables: newsletter, emails

I 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 help

INSERT INTO emails (email, name)
SELECT * FROM newsletter WHERE (emailaddress, firstname)

i tryed closing it with ";
Link to comment
https://forums.phpfreaks.com/topic/11588-from-where/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43674
Share on other sites

[!--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 reply
God bless
Link to comment
https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43677
Share on other sites

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 ()
Link to comment
https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43681
Share on other sites

[!--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.
Link to comment
https://forums.phpfreaks.com/topic/11588-from-where/#findComment-43685
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.