Unix Technical Forum

New error to me...

This is a discussion on New error to me... within the MySQL General forum forums, part of the MySQL category; --> I was trying to reduce a set of queries and ran into this: insert into forums (forum_reply_id, forum_dev_id, forum_subject, ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:49 AM
Steffan A. Cline
 
Posts: n/a
Default New error to me...

I was trying to reduce a set of queries and ran into this:


insert into forums (forum_reply_id, forum_dev_id, forum_subject,
forum_message) values (0, 1, (select forum_subject from forums where
forum_id=3 ), "I figured this one needed a reply too.")

yields:

error: You can't specify target table from 'forums' for update in from
clause

I read somewhere online when they referenced 4.x that said you cannot do
subqueries in an update. Is this true of 5.x ?

Thanks

Steffan

---------------------------------------------------------------
T E L 6 0 2 . 5 7 9 . 4 2 3 0 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
Steffan A. Cline
Steffan@ExecuChoice.net Phoenix, Az
http://www.ExecuChoice.net USA
AIM : SteffanC ICQ : 57234309
Lasso Partner Alliance Member
---------------------------------------------------------------



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:49 AM
Mogens Melander
 
Posts: n/a
Default Re: New error to me...


On Sun, July 15, 2007 03:53, Steffan A. Cline wrote:
> I was trying to reduce a set of queries and ran into this:
>
>
> insert into forums (forum_reply_id, forum_dev_id, forum_subject,
> forum_message) values (0, 1, (select forum_subject from forums where
> forum_id=3 ), "I figured this one needed a reply too.")


Your subselect "could" return more than one row, and because of that,
can't be used in a direct insert/update.

>
> yields:
>
> error: You can't specify target table from 'forums' for update in from
> clause
>
> I read somewhere online when they referenced 4.x that said you cannot do
> subqueries in an update. Is this true of 5.x ?
>
> Thanks
>
> Steffan
>
> ---------------------------------------------------------------
> T E L 6 0 2 . 5 7 9 . 4 2 3 0 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
> Steffan A. Cline
> Steffan@ExecuChoice.net Phoenix, Az
> http://www.ExecuChoice.net USA
> AIM : SteffanC ICQ : 57234309
> Lasso Partner Alliance Member
> ---------------------------------------------------------------
>
>
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=mogens@fumlersoft.dk
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>



--
Later

Mogens Melander
+45 40 85 71 38
+66 870 133 224



--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:49 AM
Steffan A. Cline
 
Posts: n/a
Default Re: New error to me...

Mogens ,

forum_id is a primary auto increment key therefore it's unlikely that there
would be a dupe. I can see there being an error if there is a dupe but in
this case there is not.


Is it really necessary to do this:

set @x = (select forum_subject from forums where forum_id=3 );

insert into forums
(project_id, forum_reply_id, forum_dev_id, forum_subject, forum_message)
values
(42, 6, 1, @x, "I figured this one needed a reply too.");

?



Thanks

Steffan

---------------------------------------------------------------
T E L 6 0 2 . 5 7 9 . 4 2 3 0 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
Steffan A. Cline
Steffan@ExecuChoice.net Phoenix, Az
http://www.ExecuChoice.net USA
AIM : SteffanC ICQ : 57234309
Lasso Partner Alliance Member
---------------------------------------------------------------



> From: Mogens Melander <mogens@fumlersoft.dk>
> Date: Sun, 15 Jul 2007 04:02:18 +0200 (CEST)
> To: "Steffan A. Cline" <steffan@hldns.com>
> Cc: <mysql@lists.mysql.com>
> Subject: Re: New error to me...
>
>
> On Sun, July 15, 2007 03:53, Steffan A. Cline wrote:
>> I was trying to reduce a set of queries and ran into this:
>>
>>
>> insert into forums (forum_reply_id, forum_dev_id, forum_subject,
>> forum_message) values (0, 1, (select forum_subject from forums where
>> forum_id=3 ), "I figured this one needed a reply too.")

>
> Your subselect "could" return more than one row, and because of that,
> can't be used in a direct insert/update.
>
>>
>> yields:
>>
>> error: You can't specify target table from 'forums' for update in from
>> clause
>>
>> I read somewhere online when they referenced 4.x that said you cannot do
>> subqueries in an update. Is this true of 5.x ?
>>
>> Thanks
>>
>> Steffan
>>
>> ---------------------------------------------------------------
>> T E L 6 0 2 . 5 7 9 . 4 2 3 0 | F A X 6 0 2 . 9 7 1 . 1 6 9 4
>> Steffan A. Cline
>> Steffan@ExecuChoice.net Phoenix, Az
>> http://www.ExecuChoice.net USA
>> AIM : SteffanC ICQ : 57234309
>> Lasso Partner Alliance Member
>> ---------------------------------------------------------------
>>
>>
>>
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe: http://lists.mysql.com/mysql?unsub=mogens@fumlersoft.dk
>>
>>
>> --
>> This message has been scanned for viruses and
>> dangerous content by MailScanner, and is
>> believed to be clean.
>>

>
>
> --
> Later
>
> Mogens Melander
> +45 40 85 71 38
> +66 870 133 224
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=steffan@hldns.com
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 06:49 AM
Paul DuBois
 
Posts: n/a
Default Re: New error to me...

At 6:53 PM -0700 7/14/07, Steffan A. Cline wrote:
>I was trying to reduce a set of queries and ran into this:
>
>
>insert into forums (forum_reply_id, forum_dev_id, forum_subject,
>forum_message) values (0, 1, (select forum_subject from forums where
>forum_id=3 ), "I figured this one needed a reply too.")
>
>yields:
>
>error: You can't specify target table from 'forums' for update in from
>clause
>
>I read somewhere online when they referenced 4.x that said you cannot do
>subqueries in an update. Is this true of 5.x ?


You can do subqueries *unless* you're selecting from the same table
that you're inserting into.

http://dev.mysql.com/doc/refman/5.0/...rt-select.html

(You're not quite using INSERT INTO ... SELECT syntax, but what
you're doing appears to be effectively equivalent.)

--
Paul DuBois, MySQL Documentation Team
Madison, Wisconsin, USA
MySQL AB, www.mysql.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:21 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com