mysql中的语法错误?

时间:2012-05-02 13:41:52

标签: mysql syntax

我已经习惯了MS sql,所以使用mySql肯定会给我一些语法错误,这肯定会让我失望。我有以下内容:

declare @expensesSum DOUBLE
select @expensesSum = sum(expenseAmount) from bondsaverdb.expenses
insert into bondsaverdb.expenses
select '6','Extracash',(income - @expensesSum)  from bondsaverdb.income where userName ='Dean'

我得到的错误说:

  

声明@expensesSum

附近的语法错误

必须是我声明变量的方式??

提前致谢!

2 个答案:

答案 0 :(得分:1)

MySQL不需要(或者甚至允许)显示declare个变量,据我所知,它会将所有对象,包含的变量视为字符串 - 因此无法输入办法。无论如何,这应该工作:

SET @expensesSum = (SELECT SUM(expenseAmount) FROM bondsaverdb.expenses);
INSERT INTO
   bondsaverdb.expenses
SELECT
   '6', 'Extracash', (income - @expensesSum)
FROM
   bondsaverdb.income
WHERE
   userName = 'Dean'

我也不确定'6'是什么意思..如果这是一个自动增量ID,你应该完全从INSERT中省略它(并指定要写入的其他列)或离开它NULL

答案 1 :(得分:1)

MySQL与SQL Server非常不同;你通常不会声明变量

SET @expensesSum = (select @expensesSum = sum(expenseAmount) from bondsaverdb.expenses);
insert into bondsaverdb.expenses
select '6','Extracash',(income - @expensesSum)  from bondsaverdb.income where userName ='Dean';