Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14869

insert a value calculated from another table

I would like to do a query for inserting a value calculated starting upon another table. I don't want to use Temporary tables and I would like to do everything in one singel query. It is possible?

I tried this one below but it doesn't work.

Thanks

AFeG

INSERT INTO MyTable( `DATE`, `Name`, `Total` ) 
VALUES ( 
 '2010/01/01',
 'Thunder',
  SELECT SUM(aValue) FROM AnotherTable
)

Upvotes: 2

Views: 10569

Answers (4)

gretarsson
gretarsson

Reputation: 41

I think the INSERT INTO statement simply failed because the nested SELECT statement has to be enclosed in parentheses:

INSERT INTO MyTable( `DATE`, `Name`, `Total` ) 
VALUES ( 
 '2010/01/01',
 'Thunder',
  (SELECT SUM(aValue) FROM AnotherTable)
);

Upvotes: 0

Liz Albin
Liz Albin

Reputation: 1499

If you want precisely what you indicated:

insert mytable
select 
"2010/01/01", "thunder", 
sum(mycolumn)
from othertable

Upvotes: 0

Quassnoi
Quassnoi

Reputation: 425633

INSERT  
INTO    MyTable( `DATE`, `Name`, `Total` ) 
SELECT  '2010/01/01', 'Thunder', SUM(aValue)
FROM    AnotherTable

Upvotes: 3

Daniel
Daniel

Reputation: 11064

Try

insert mytable
select date
      ,name
      ,sum(total)
  from anothertable

Upvotes: 1

Related Questions