Reputation: 14869
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
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
Reputation: 1499
If you want precisely what you indicated:
insert mytable
select
"2010/01/01", "thunder",
sum(mycolumn)
from othertable
Upvotes: 0
Reputation: 425633
INSERT
INTO MyTable( `DATE`, `Name`, `Total` )
SELECT '2010/01/01', 'Thunder', SUM(aValue)
FROM AnotherTable
Upvotes: 3
Reputation: 11064
Try
insert mytable
select date
,name
,sum(total)
from anothertable
Upvotes: 1