Hi all, I just created a simple database in MySQL, and I am learning how to write stored proc's. I'm familiar with M$SQL and as far as I can see the following should work:
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 ;
instead i get the following
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.
What am I doing wrong? I seem to have exhausted my limited mental abilities!
After following suggestions about removing the insert ... select mapping and re-run, I get the following error
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.