MySQL创建存储过程失败但所有内部查询单独成功?

时间:2010-06-07 23:52:33

标签: mysql stored-procedures

我刚在MySQL中创建了一个简单的数据库,我正在学习如何编写存储过程。我熟悉M $ SQL,据我所知,以下内容应该有效:

use mydb;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER //
CREATE PROCEDURE mydb.doStats ()
BEGIN

CREATE TABLE IF NOT EXISTS resultprobability (
 ballNumber INT NOT NULL ,
 probability FLOAT NULL,
 PRIMARY KEY (ballNumber) );

CREATE TABLE IF NOT EXISTS drawProbability (
 drawDate DATE NOT NULL ,
 ball1 INT NULL ,
 ball2 INT NULL ,
 ball3 INT NULL ,
 ball4 INT NULL ,
 ball5 INT NULL ,
 ball6 INT NULL ,
 ball7 INT NULL ,
 score FLOAT NULL ,
 PRIMARY KEY (drawDate) );

TRUNCATE TABLE resultprobability;

TRUNCATE TABLE drawprobability;

INSERT INTO resultprobability (ballNumber, probability)
(select resultset.ballNumber ballNumber,(count(0)/(select count(0) from resultset)) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO drawProbability (drawDate, ball1, ball2, ball3, ball4, ball5, ball6, ball7, score)
(select distinct r.drawDate, a.ballnumber ball1, b.ballnumber ball2,
 c.ballnumber ball3, d.ballnumber ball4, e.ballnumber ball5,
 f.ballnumber ball6,g.ballnumber ball7,
((a.probability + b.probability + c.probability + d.probability + e.probability + f.probability + g.probability)/7) score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 1) a on a.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 2) b on b.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 3) c on c.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 4) d on d.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 5) e on e.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 6) f on f.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 7) g on g.drawdate = r.drawDate
 order by score desc);

END
//
DELIMITER ;

而是我得到以下

Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26
Line 6, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO d' at line 1
Line 31, column 51

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114

Execution finished after 0.002 s, 3 error(s) occurred.

我做错了什么?我似乎已经筋疲力尽了我的精神能力!

在遵循有关删除插入的建议后...选择映射并重新运行,我收到以下错误

Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26
Line 6, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO d' at line 1
Line 31, column 51

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114

Execution finished after 0.002 s, 3 error(s) occurred.

1 个答案:

答案 0 :(得分:1)

“所有内部查询单独成功”

不,他们没有,这是第一个常见的冒犯,但是:

INSERT INTO drawProbability (...) (select...

丢失'('在选择之前,重新运行,重新测试,解决下一个语法问题。渴望解决方案是没有理由跳过基本错误检查,特别是如果你没有,则不要声称已经运行它们。