tags:

views:

1745

answers:

5

Clearly the following is incorrect.

INSERT INTO `aTable` (`A`,`B`) VALUES((SELECT MAX(`A`) FROM `aTable`)*2),'name');

I get the value:

SQL query:

INSERT INTO `aTable` (`A`, `B` )
VALUES 
(
  (
   SELECT MAX(`A`)
   FROM `aTable`
  ) *2
 , 'name'
)

MySQL said:

1093 - You can't specify target table 'aTable' for update in FROM clause

So, I'm trying to make a bitmap table, each row corresponds to one Bit, and has a 'map' value.

To insert in the table, I don't want to do two queries, I want to do one. How should I do this?

No one commented on this, but since I am trying to make a bitmap, it should be * 2 not ^ 2, my mistake, please note that is why the comments often say ^ 2, it was an error in the version that the commenters read.

+2  A: 

I think you need to drop the "VALUES", and have a valid select statement.

see this link

I'm not particularly a mySQL guy, I use MSSQL mostly. But If you format the select statement correctly, It should work.

stephenbayer
No, I'm trying to select from the same table as the insert, and MySQL doesn't like that..Sory, I hadn't typed the query in correctly the first time, but the Select was still correct.
Issac Kelly
hrm. Okay, does mySQL have user defined functions? So you can create a function that returns the current "MAX(`A`)" and use it in your statement?
stephenbayer
It does (stored procedures) but I'm unsure if it will let you do that still. Worth a try
Issac Kelly
I'm watching these comments, try it and and let me know if it works. If not I'll set up some similar tables on my local mySQL during lunch and try to see if I can get something to work, if no one with better mySQL knowledge hasn't got an answer yet.
stephenbayer
Hrm. I was not able to come up with a satisfactory solution due to my inability to use mySQL. It appears to me mySQL is lacking in some abilities that MSSQL has, but that just might be my inexperience in that dbms.
stephenbayer
A: 

as soon as the Select is correct you can do this.

Enreeco
If I run the select as a query by itself, it works fine.
Issac Kelly
+2  A: 

I take it that INSERT ... SELECT isn't working? I see this in the documentation for it:

The target table of the INSERT statement may appear in the FROM clause of the SELECT part of the query. (This was not possible in some older versions of MySQL.) In this case, MySQL creates a temporary table to hold the rows from the SELECT and then inserts those rows into the target table.

Out of curiosity, which version of MySQL are you using?

R. Bemrose
The next suggestion would be, if you're using an older version of MySQL, use a temporary table, which is what current versions of MySQL do implicitly.
Bill Karwin
..it should be working# Server version: 5.0.45-Debian_1ubuntu3.3-log# Protocol version: 10
Issac Kelly
A: 

I think your statement should look like this...

INSERT INTO `aTable` (`A`,`B`) 
SELECT MAX(`A`)^2,'name' FROM `aTable`
Wayne
nope, i was thinking that to. but `A` is from `aTable` and `name` is from Groups, that SQL statement won't work
stephenbayer
+3  A: 

try:

insert into aTable select max(a)^2, 'name' from aTable;

or

insert into aTable select max(a)^2, 'name' from aTable group by B;

If you need a join, you can do this:

insert into aTable select max(a)^2, 'name' from aTable, bTable;

My "Server version" is "5.0.51b-community-nt MySQL Community Edition (GPL)"

Leonel Martins