Jump to content

Archived

This topic is now archived and is closed to further replies.

destined

FROM WHERE

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 ";

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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 ()

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
ok ok ok ...lol

i used the first method before comming back here and it work for what i needed to do
But now i need to get the same emailaddress/firstname from an other database (users)
table: tbl_users

Thank You both for your help

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.