'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
)
Solution 1:[1]
INSERT
INTO MyTable( `DATE`, `Name`, `Total` )
SELECT '2010/01/01', 'Thunder', SUM(aValue)
FROM AnotherTable
Solution 2:[2]
Try
insert mytable
select date
,name
,sum(total)
from anothertable
Solution 3:[3]
If you want precisely what you indicated:
insert mytable
select
"2010/01/01", "thunder",
sum(mycolumn)
from othertable
Solution 4:[4]
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)
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Quassnoi |
| Solution 2 | Daniel |
| Solution 3 | Liz Albin |
| Solution 4 | gretarsson |
